-
Notifications
You must be signed in to change notification settings - Fork 383
/
ascii2hex.sv
52 lines (40 loc) · 1.41 KB
/
ascii2hex.sv
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
//------------------------------------------------------------------------------
// hex2ascii.sv
// published as part of https://github.com/pConst/basic_verilog
// Konstantin Pavlov, pavlovconst@gmail.com
//------------------------------------------------------------------------------
// INFO ------------------------------------------------------------------------
// Converts 8-bit human-readable ASCII char to 4-bit binary nibble
// For example, "F" char becomes 4'b1111, "4" char becomes 4'b0100
//
/* --- INSTANTIATION TEMPLATE BEGIN ---
ascii2hex AH (
.ascii( ),
.hex( )
);
--- INSTANTIATION TEMPLATE END ---*/
module ascii2hex (
input [7:0] ascii,
output [3:0] hex
);
always_comb begin
case( ascii[7:0] )
8'd48 : hex[3:0] = 4'h0;
8'd49 : hex[3:0] = 4'h1;
8'd50 : hex[3:0] = 4'h2;
8'd51 : hex[3:0] = 4'h3;
8'd52 : hex[3:0] = 4'h4;
8'd53 : hex[3:0] = 4'h5;
8'd54 : hex[3:0] = 4'h6;
8'd55 : hex[3:0] = 4'h7;
8'd56 : hex[3:0] = 4'h8;
8'd57 : hex[3:0] = 4'h9;
8'd65, 8'd97 : hex[3:0] = 4'hA; // lowercase and capital letters
8'd66, 8'd98 : hex[3:0] = 4'hB;
8'd67, 8'd99 : hex[3:0] = 4'hC;
8'd68, 8'd100: hex[3:0] = 4'hD;
8'd69, 8'd101: hex[3:0] = 4'hE;
8'd70, 8'd102: hex[3:0] = 4'hF;
default : hex[3:0] = 4'h0;
endcase
endmodule