FONT SIZE : AAA
We now have a way to display four digits on the LEDs, but we need to specify which digit we want to display on each separately. It would be nice to be able to specify a number that would get displayed across all four digits. To do this, we need to convert the binary number from the DIP switches into a decimal number.
Technically, the number will still be in binary since that is all we have to work with, but we will produce a separate number for each digit from a single binary number.
Luckily, there is a component to do this for us. Go to the Components Library and add the Binary to Decimal component, which can be found in the Miscellaneous sec‐ tion.
This component will output the decimal numbers, assuming our input is valid. It uses the special value of 10 to indicate the digit should be blank (if there shouldn’t be lead‐ ing zeros), and the value of 11 to indicate that there was overflow. It is a bit compli‐ cated, but let’s break it down:
module bin_to_dec #(
DIGITS = 1 : DIGITS > 0,
LEADING_ZEROS = 0 : LEADING_ZEROS == 0 || LEADING_ZEROS == 1
)(
/ minimum number of bits for DIGITS
input value[$clog2($pow(10, DIGITS))],
output digits[DIGITS][4] / decimal output
) {
var i, j, scale;
sig remainder[value.WIDTH]; / running remainder
/ temporary subtraction value
sig sub_value[value.WIDTH];
sig blank; / flag for leading zeros
always {
for (i = 0; i < DIGITS; i++) / for all digits
/ default to invalid number
digits[i] = d11;
remainder = value; / initialize remainder
blank = !LEADING_ZEROS; / set blank zero flag
if (value < $pow(10, DIGITS)) { / if can be displayed
for (j = DIGITS - 1; j >= $signed(0); j--) { / for each digit
/ get the scale for the digit
scale = $pow(10, j);
if (remainder < scale) { / if this digit is 0
if (j != 0 && blank) / use 10 for blank
digits[j] = 10;
else / or 0 for zero
digits[j] = 0;
} else { / digit is 1-9
blank = 0; / don't blank future zeros
/ default to no subtraction
sub_value = 0;
for (i = 9; i >= 1; i--) { / for each possible value
/ if remainder is less than value
if (remainder < (i+1) * scale) {
digits[j] = i; / set digit to this value
sub_value = i * scale; / set subtraction value
}
}
remainder = remainder - sub_value; / subtract off last digit
}
}
}
}
}
The two parameters should be fairly straightforward at this point. DIGITS specifies the number of digits the decimal number can have, and LEADING_ZEROS specifies that 0 or 10 should be used for leading zeros. Basically, since 10 isn’t a decimal digit, we can use this as a blank digit value. For a number such as 15 with LEADING_ZEROS set to 1 and DIGITS set to 4, we would get 0015, but with LEADING_ZEROS set to 0, we get 15.
The size of the input value is a bit complicated. The function $pow(a,b) returns ab. So here we are using it to get the maximum value of a decimal number with DIGITS digits can have, plus 1. For example, if DIGITS is 2, the maximum value is 99, and 102 is 100. The $clog2() function is then used to tell us the minimum number of bits needed to store the maximum value plus 1, minus 1—so really just the minimum bits needed to store the maximum value.
Inside an always block, the types sig and var can be read and written, and their value can change throughout the block. It is best to think of sig as a wire. However, when you start using them in for loops, they will actually be multiple wires that hap‐ pen to use the same name. Take a look at Figure 5-3 for one way this module could be implemented.
This schematic leaves out the leading zero and overflow detection parts of the design but still illustrates how you could make this module. The part shown would be the part responsible for detecting the 10s place digit. Each digit would have a copy of this circuit, except the constants would be different. Instead of multiples of 10, they would be multiples of that digit’s power: 1,000, 100, 10, 1, and so forth.
The first part of the schematic consists of a bank of comparators that detect whether the input is less than all the possible digits (multiplied by the power of this section). These are then fed into an arbiter that will select the smallest number that is still larger than the input. For example, if the input is 24, the arbiter would select the < 30 comparator.
The output from the arbiter is then used to select the digit via a multiplexer being used as a lookup table.
We also need to output the remainder for the next digit, so we then have to subtract off the current digit. Which value we use is selected using the output of the arbiter again but with another multiplexer. If we have 24 as the input, then the 20 would be selected and we would subtract that from the input. The remainder would be 4 and that would get passed to the next stage.
The nice part about this design is that it doesn’t use any division. It uses comparisons and subtraction, which are relatively cheap when compared to division.
Note that this is only one way you could realize this design. The tools may take a completely different route that happens to be more efficient. It is, however, important to try to visualize how a circuit you are designing could be realized. This will help you design efficient circuits that are easily realized in hardware.With an understanding of this component, let’s put it to use. Instantiate it in mojo_top:
.clk(clk) {
/ The reset conditioner is used to synchronize the reset signal to the FPGA
/ clock. This ensures the entire FPGA comes out of reset at the same time.
reset_conditioner reset_cond;
.rst(rst) {
multi_seven_seg seg_display;
counter ctr (#TOP(9999), #DIV(22), #SIZE(14));
}
}
bin_to_dec digits (#DIGITS(4));
Also note the counter is back and the parameters changed so that it will count from 0–9999.
Then connect it all together in the always block:
digits.value = ctr.value;
seg_display.values = digits.digits;
seg_display.decimal = 4b0000;
io_seg = ~seg_display.seg;
io_sel = ~seg_display.sel;
Build and load the project, and the LED displays should show a counter.
Manufacturer:Xilinx
Product Categories: FPGAs
Lifecycle:Active Active
RoHS: No RoHS
Manufacturer:Xilinx
Product Categories:
Lifecycle:Any -
RoHS: -
Manufacturer:Xilinx
Product Categories: FPGAs (Field Programmable Gate Array)
Lifecycle:Active Active
RoHS:
Manufacturer:Xilinx
Product Categories:
Lifecycle:Any -
RoHS: -
Manufacturer:Xilinx
Product Categories: FPGAs
Lifecycle:Obsolete -
RoHS: No RoHS
Support