2 min readByAleksandr SidunAleksandr Sidun
Suggest Edit

Decimal-to-Binary Encoder

This is a static combinatorial block that converts a fixed decimal value (set via the decimal_number parameter) into a parallel binary output bus — there is no input port and no clock, so the output is driven once at simulation start. It is most useful as a one-shot bit-pattern generator when you need to pre-load a specific digital code into a DAC, decoder, or register model at the beginning of a simulation. The binary_bits macro sets the bus width and vdd/vss define the output logic levels. A typical use-case is quickly applying a known static code to a DAC model to verify its output voltage at a specific operating point, without wiring up a counter or sequencer stimulus.

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.

Cell name: dec2bin

Model type: Verilog-A

verilog
1// Decimal number to binary code converter 2// LSB is [0] 3// Change binary_bits variable for your needs! 4// Source: AnalogHub.ie 5 6`include "constants.vams" 7`include "disciplines.vams" 8`define binary_bits 8 // define number of binary bits here 9 10module dec2bin(out); 11output [`binary_bits-1:0] out; 12voltage [`binary_bits-1:0] out; 13 14parameter real decimal_number = 5; 15parameter real vdd = 1.0; // voltage level of logic 1 (V) 16parameter real vss = 0; // voltage level of logic 0 (V) 17real dout[`binary_bits-1:0]; // internal result variable 18genvar i; 19real x; 20real z; 21 22analog begin 23// Converting decimal to binary using modulus of 2 24 x = decimal_number; 25while (x!=0) begin 26 for (i = 0; i <`binary_bits; i = i + 1) begin 27 z = x/2.0; 28 x = floor(z); 29 dout[i] = ceil(z - x); 30 end 31end 32 33// Plotting outputs 34for (i=0; i<`binary_bits; i=i+1) 35 V(out[i]) <+ transition(dout[i]*vdd,0,0); 36end 37 38endmodule