Created Feb 24, 2026Updated Feb 24, 2026Aleksandr SidunSuggest Edit

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.

Cell name: dec2bin

Model type: Verilog-A

Download from Github

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\t\t\t\t// 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;\t\t\t// voltage level of logic 1 (V) 17parameter real vss = 0;\t\t\t\t// voltage level of logic 0 (V) 18real dout[\`binary_bits-1:0];\t\t// internal result variable 19genvar i; 20real x; 21real z; 22 23analog begin 24// Converting decimal to binary using modulus of 2 25\tx = decimal_number; 26while (x!=0) begin 27\tfor (i = 0; i <\`binary_bits; i = i + 1) begin 28\t\tz = x/2.0; 29\t\tx = floor(z);\t 30\t\tdout[i] = ceil(z - x); 31\tend 32end 33 34// Plotting outputs 35for (i=0; i<\`binary_bits; i=i+1) 36\t V(out[i]) <+ transition(dout[i]*vdd,0,0); 37end 38 39endmodule