-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcd.sv
More file actions
121 lines (109 loc) · 2.3 KB
/
Copy pathbcd.sv
File metadata and controls
121 lines (109 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
bcd.sv
Aaron Shappell, 1739332
Ryan Shappell, 1739333
3/11/2020
EE371
Lab 6, Space Invaders
This lab is the final project, we chose to implement the classic game Space Invaders on the DE1_SoC.
*/
// Handles binary to BCD conversion for the given input number into 5 digits
module bcd(clk, reset, in, out);
// Input and output logic
input logic clk, reset;
input logic [14:0] in;
output logic [19:0] out;
// Internal logic
logic [14:0] bin;
logic [3:0] ones, tens, huns, thous, tenthous;
logic [3:0] done;
// States
enum {IDLE, ADD, SHIFT} ps, ns;
// Next state logic
always_comb begin
case (ps)
IDLE:
ns = ADD;
ADD:
ns = SHIFT;
SHIFT:
if (done == 0) ns = IDLE;
else ns = ADD;
endcase
end
// Sequential DFF logic
always_ff @(posedge clk) begin
if(reset) begin
ps <= IDLE;
bin <= 0;
out <= 0;
ones <= 0;
tens <= 0;
huns <= 0;
thous <= 0;
tenthous <= 0;
done <= 4'b1110;
end else begin
ps <= ns;
// Reset digit cols, latch input and output
if (ps == IDLE) begin
bin <= in;
done <= 4'b1110;
ones <= 0;
tens <= 0;
huns <= 0;
thous <= 0;
tenthous <= 0;
out <= {tenthous, thous, huns, tens, ones};
end
// Add 3 to each digit >= 5
if (ps == ADD) begin
if (ones >= 5)
ones <= ones + 3;
if (tens >= 5)
tens <= tens + 3;
if (huns >= 5)
huns <= huns + 3;
if (thous >= 5)
thous <= thous + 3;
if (tenthous >= 5)
tenthous <= tenthous + 3;
end
// Shift binary representation to digit cols
if (ps == SHIFT) begin
tenthous <= (tenthous << 1) | thous[3];
thous <= (thous << 1) | huns[3];
huns <= (huns << 1) | tens[3];
tens <= (tens << 1) | ones[3];
ones <= (ones << 1) | bin[14];
bin <= bin << 1;
done <= done - 1'b1;
end
end
end
endmodule
/*
Tests all possible inputs of the bcd module to verify the outputs and functionality.
*/
module bcd_tb();
// Test logic
logic clk, reset;
logic [14:0] in;
logic [19:0] out;
// Device under test
bcd dut (.*);
// Setup clock
parameter CLOCK_PERIOD = 100;
initial begin
clk <= 0;
forever #(CLOCK_PERIOD / 2) clk <= ~clk;
end
// Test inputs
initial begin
reset <= 1; @(posedge clk);
reset <= 0; in <= 12345;
for (int i = 0; i < 100; i++)
@(posedge clk);
$stop;
end
endmodule