]>
Commit | Line | Data |
---|---|---|
1 | // convert ASCII 0-9A-Za-z to/from numbers | |
2 | ||
3 | module ascii_to_hex(input [7:0] ascii, output [3:0] hex); | |
4 | wire [7:0] hex8; | |
5 | assign hex8 = | |
6 | ascii > 8'd47 & ascii < 8'd58 ? ascii - 8'd48 : | |
7 | ascii > 8'd64 & ascii < 8'd71 ? ascii - 8'd55 : | |
8 | ascii > 8'd96 & ascii < 8'd103 ? ascii - 8'd87 : 8'bxxxx; | |
9 | assign hex = hex8[3:0]; | |
10 | ||
11 | /* assign hex = | |
12 | ascii == 8'd48 ? 4'd0 : | |
13 | ascii == 8'd49 ? 4'd1 : | |
14 | ascii == 8'd50 ? 4'd2 : | |
15 | ascii == 8'd51 ? 4'd3 : | |
16 | ascii == 8'd52 ? 4'd4 : | |
17 | ascii == 8'd53 ? 4'd5 : | |
18 | ascii == 8'd54 ? 4'd6 : | |
19 | ascii == 8'd55 ? 4'd7 : | |
20 | ascii == 8'd56 ? 4'd8 : | |
21 | ascii == 8'd57 ? 4'd9 : | |
22 | ascii == 8'd65 ? 4'd10 : | |
23 | ascii == 8'd66 ? 4'd11 : | |
24 | ascii == 8'd67 ? 4'd12 : | |
25 | ascii == 8'd68 ? 4'd13 : | |
26 | ascii == 8'd69 ? 4'd14 : 4'd15;*/ | |
27 | endmodule // ascii_to_hex | |
28 | ||
29 | module hex_to_ascii(input [3:0] hex, output [7:0] ascii); | |
30 | // assign ascii = hex < 4'd10 ? 8'd48 + hex : 8'd55 + hex; | |
31 | assign ascii = | |
32 | hex == 4'd0 ? 8'd48 : | |
33 | hex == 4'd1 ? 8'd49 : | |
34 | hex == 4'd2 ? 8'd50 : | |
35 | hex == 4'd3 ? 8'd51 : | |
36 | hex == 4'd4 ? 8'd52 : | |
37 | hex == 4'd5 ? 8'd53 : | |
38 | hex == 4'd6 ? 8'd54 : | |
39 | hex == 4'd7 ? 8'd55 : | |
40 | hex == 4'd8 ? 8'd56 : | |
41 | hex == 4'd9 ? 8'd57 : | |
42 | hex == 4'd10 ? 8'd65 : | |
43 | hex == 4'd11 ? 8'd66 : | |
44 | hex == 4'd12 ? 8'd67 : | |
45 | hex == 4'd13 ? 8'd68 : | |
46 | hex == 4'd14 ? 8'd69 : | |
47 | hex == 4'd15 ? 8'd70 : 8'bx; | |
48 | endmodule // hex_to_ascii |