Skip to content

Commit 8b1b2ef

Browse files
committed
Added ASCII-to-HEX
1 parent eb72143 commit 8b1b2ef

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

ascii2hex.sv

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//------------------------------------------------------------------------------
2+
// hex2ascii.sv
3+
// published as part of https://github.com/pConst/basic_verilog
4+
// Konstantin Pavlov, [email protected]
5+
//------------------------------------------------------------------------------
6+
7+
// INFO ------------------------------------------------------------------------
8+
// Converts 8-bit human-readable ASCII char to 4-bit binary nibble
9+
// For example, "F" char becomes 4'b1111, "4" char becomes 4'b0100
10+
//
11+
12+
13+
/* --- INSTANTIATION TEMPLATE BEGIN ---
14+
15+
ascii2hex AH (
16+
.ascii( ),
17+
.hex( )
18+
);
19+
20+
--- INSTANTIATION TEMPLATE END ---*/
21+
22+
23+
module ascii2hex (
24+
input [7:0] ascii,
25+
output [3:0] hex
26+
);
27+
28+
always_comb begin
29+
case( ascii[7:0] )
30+
8'd48 : hex[3:0] = 4'h0;
31+
8'd49 : hex[3:0] = 4'h1;
32+
8'd50 : hex[3:0] = 4'h2;
33+
8'd51 : hex[3:0] = 4'h3;
34+
8'd52 : hex[3:0] = 4'h4;
35+
8'd53 : hex[3:0] = 4'h5;
36+
8'd54 : hex[3:0] = 4'h6;
37+
8'd55 : hex[3:0] = 4'h7;
38+
8'd56 : hex[3:0] = 4'h8;
39+
8'd57 : hex[3:0] = 4'h9;
40+
41+
8'd65, 8'd97 : hex[3:0] = 4'hA; // lowercase and capital letters
42+
8'd66, 8'd98 : hex[3:0] = 4'hB;
43+
8'd67, 8'd99 : hex[3:0] = 4'hC;
44+
8'd68, 8'd100: hex[3:0] = 4'hD;
45+
8'd69, 8'd101: hex[3:0] = 4'hE;
46+
8'd70, 8'd102: hex[3:0] = 4'hF;
47+
48+
default : hex[3:0] = 4'h0;
49+
endcase
50+
51+
endmodule
52+

0 commit comments

Comments
 (0)