1 |
11 |
acapola |
/*
|
2 |
|
|
Author: Sebastien Riou (acapola)
|
3 |
|
|
Creation date: 17:16:40 01/09/2011
|
4 |
|
|
|
5 |
|
|
$LastChangedDate: 2011-02-10 16:40:57 +0100 (Thu, 10 Feb 2011) $
|
6 |
|
|
$LastChangedBy: acapola $
|
7 |
|
|
$LastChangedRevision: 14 $
|
8 |
|
|
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/test/HexStringConversion.v $
|
9 |
|
|
|
10 |
|
|
This file is under the BSD licence:
|
11 |
|
|
Copyright (c) 2011, Sebastien Riou
|
12 |
|
|
|
13 |
|
|
All rights reserved.
|
14 |
|
|
|
15 |
|
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
16 |
|
|
|
17 |
|
|
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
18 |
|
|
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
19 |
|
|
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
|
20 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21 |
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22 |
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23 |
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
24 |
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
25 |
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
26 |
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
27 |
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28 |
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29 |
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30 |
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 |
|
|
*/
|
32 |
9 |
acapola |
|
33 |
|
|
function [7:0] hexString2Byte;
|
34 |
|
|
input [15:0] byteInHexString;
|
35 |
|
|
integer i;
|
36 |
|
|
reg [7:0] hexDigit;
|
37 |
|
|
reg [4:0] nibble;
|
38 |
|
|
begin
|
39 |
|
|
for(i=0;i<2;i=i+1) begin
|
40 |
|
|
nibble=5'b10000;//invalid
|
41 |
|
|
hexDigit=byteInHexString[i*8+:8];
|
42 |
|
|
if(("0"<=hexDigit)&&("9">=hexDigit))
|
43 |
|
|
nibble=hexDigit-"0";
|
44 |
|
|
if(("a"<=hexDigit)&&("f">=hexDigit))
|
45 |
|
|
nibble=10+hexDigit-"a";
|
46 |
|
|
if(("A"<=hexDigit)&&("F">=hexDigit))
|
47 |
|
|
nibble=10+hexDigit-"A";
|
48 |
|
|
if(nibble>15) begin
|
49 |
|
|
$display("Invalid input for hex conversion: '%s', hexDigit='%s' (%x), nibble=%d",byteInHexString,hexDigit,hexDigit,nibble);
|
50 |
|
|
$finish;
|
51 |
|
|
end
|
52 |
|
|
hexString2Byte[i*4+:4]=nibble;
|
53 |
|
|
end
|
54 |
|
|
end
|
55 |
|
|
endfunction
|
56 |
|
|
|
57 |
14 |
acapola |
task getNextHexByte;
|
58 |
|
|
input [8*3*257:0] bytesString;
|
59 |
|
|
input integer indexIn;
|
60 |
|
|
output reg [7:0] byteOut;
|
61 |
|
|
output integer indexOut;
|
62 |
|
|
reg [15:0] byteInHex;
|
63 |
|
|
begin
|
64 |
|
|
byteInHex=" ";
|
65 |
|
|
//$display("bytesString: %x",bytesString);
|
66 |
|
|
while((indexIn>=16)&((8'h0==byteInHex[15:8])|(8'h20==byteInHex[15:8]))) begin
|
67 |
|
|
byteInHex=bytesString[(indexIn-1)-:16];
|
68 |
|
|
indexIn=indexIn-8;
|
69 |
|
|
//$display("indexIn: %d",indexIn);
|
70 |
|
|
end
|
71 |
|
|
indexOut=indexIn-8;
|
72 |
|
|
//$display("indexOut: %d, byteInHex: '%s' (%x)",indexOut, byteInHex, byteInHex);
|
73 |
|
|
if((16'h0!=byteInHex) & (indexOut>=0) & (8'h20!=byteInHex[7:0])) begin
|
74 |
|
|
byteOut=hexString2Byte(byteInHex);
|
75 |
|
|
//$display("byteOut: %x",byteOut);
|
76 |
|
|
end else begin
|
77 |
|
|
indexOut=-1;
|
78 |
|
|
end
|
79 |
|
|
end
|
80 |
|
|
endtask
|