Decimal-to-Binary Encoder

This page contains Verilog-A model of the decimal to binary encoder.

Usage:

  1. Create a new cell in Library Manager named dec2bin and select cell type Verilog A;
  2. Copy and paste the code provided;
  3. Specify binary_bits variable to be the desired binary bits number;
  4. Perform Check and Save;
  5. A cell symbol will be created;
  6. Instantiate dec2bin cell into your design;
  7. Perform Check and Save and run the simulation.
1// Decimal number to binary code converter 2// LSB is [0] 3// Change binary_bits variable for your needs! 4// Author: A. Sidun 5// Source: AnalogHub.ie 6 7`include "constants.vams" 8`include "disciplines.vams" 9`define binary_bits 8 // define number of binary bits here 10 11module dec2bin(out); 12output [`binary_bits-1:0] out; 13voltage [`binary_bits-1:0] out; 14 15parameter real decimal_number = 5; 16parameter real vdd = 1.0; // voltage level of logic 1 (V) 17parameter real vss = 0; // voltage level of logic 0 (V) 18real dout[`binary_bits-1:0]; // internal result variable 19genvar i; 20real x; 21real z; 22 23analog begin 24// Converting decimal to binary using modulus of 2 25 x = decimal_number; 26while (x!=0) begin 27 for (i = 0; i <`binary_bits; i = i + 1) begin 28 z = x/2.0; 29 x = floor(z); 30 dout[i] = ceil(z - x); 31 end 32end 33 34// Plotting outputs 35for (i=0; i<`binary_bits; i=i+1) 36 V(out[i]) <+ transition(dout[i]*vdd,0,0); 37end 38 39endmodule