summaryrefslogtreecommitdiff
path: root/part_1/mylib/bin2bcd_10.v
blob: 970d33012d5c15e9e9b3df8194b336749574fbba (plain)
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
module ex4_top (
	input [9:0] SW,
	output [6:0] HEX0,
	output [6:0] HEX1,
	output [6:0] HEX2,
	output [6:0] HEX3
	);
	
	
	reg [3:0] BCD0;
	reg [3:0] BCD1;
	reg [3:0] BCD2;
	reg [3:0] BCD3;
	
	integer i;
	always @ (*)
	begin 
		BCD0 = 4'd0;
		BCD1 = 4'd0;
		BCD2 = 4'd0;
		BCD3 = 4'd0;
		
		for (i=9; i>=0; i=i-1)
		begin	
			if (BCD3 >= 5)
				BCD3 = BCD3 +3;
			if (BCD2 >= 5)
				BCD2 = BCD2 +3;
			if (BCD1 >= 5)
				BCD1 = BCD1 +3;
			if (BCD0 >= 5)
				BCD0 = BCD0 +3;
				
			// shift each digit
			BCD3 = BCD3 << 1;
			BCD3[0] = BCD2[3];
			
			BCD2 = BCD2 << 1;
			BCD2[0] = BCD1[3];
			
			BCD1 = BCD1 << 1;
			BCD1[0] = BCD0[3];
			
			BCD0 = BCD0 << 1;
			BCD0[0] = SW[i];
		end
		
	end
	
		hex_to_7seg SEG3 (HEX3, BCD3);
		hex_to_7seg SEG2 (HEX2, BCD2);
		hex_to_7seg SEG1 (HEX1, BCD1);
		hex_to_7seg SEG0 (HEX0, BCD0);
endmodule