OpenCores
URL https://opencores.org/ocsvn/iso7816_3_master/iso7816_3_master/trunk

Subversion Repositories iso7816_3_master

[/] [iso7816_3_master/] [trunk/] [test/] [DummyCard.v] - Blame information for rev 19

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 acapola
/*
2
Author: Sebastien Riou (acapola)
3
Creation date: 22:22:43 01/10/2011
4
 
5
$LastChangedDate: 2011-04-17 23:31:29 +0200 (Sun, 17 Apr 2011) $
6
$LastChangedBy: acapola $
7
$LastChangedRevision: 19 $
8
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/test/DummyCard.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 4 acapola
`default_nettype none
33 3 acapola
 
34
module DummyCard(
35 4 acapola
        input wire isoReset,
36
        input wire isoClk,
37
        input wire isoVdd,
38
        inout wire isoSio
39 3 acapola
        );
40
 
41
        // Inputs
42
        wire [0:0] clkPerCycle=0;
43
        reg [7:0] dataIn;
44
        reg nWeDataIn;
45
        reg nCsDataOut;
46
        reg nCsStatusOut;
47
 
48
        // Outputs
49 15 acapola
        wire [7:0] uart_dataOut;
50 3 acapola
        wire [7:0] statusOut;
51
        wire serialOut;
52 7 acapola
        reg [12:0] cyclesPerEtu;
53 3 acapola
 
54 4 acapola
        wire cardIsoClk;//card use its own generated clock (like true UARTs)
55 15 acapola
 
56
reg useIndirectConventionConfig;//can be changed by commands
57
reg useIndirectConvention;
58
 
59
wire stopBit2=1'b1;//0: 1 stop bit, 1: 2 stop bits 
60
wire msbFirst = useIndirectConvention;//if 1, bits order is: startBit, b7, b6, b5...b0, parity
61
wire oddParity = 1'b0;//if 1, parity bit is such that data+parity have an odd number of 1
62
wire sioHighValue = ~useIndirectConvention;//apply only to data bits
63
 
64
wire [7:0] uart_dataIn = sioHighValue ? dataIn : ~dataIn;
65
wire [7:0] dataOut = sioHighValue ? uart_dataOut : ~uart_dataOut;
66
 
67 4 acapola
        HalfDuplexUartIf uartIf (
68 3 acapola
                .nReset(isoReset),
69
                .clk(isoClk),
70
                .clkPerCycle(clkPerCycle),
71 15 acapola
                .dataIn(uart_dataIn),
72 3 acapola
                .nWeDataIn(nWeDataIn),
73 7 acapola
                .clocksPerBit(cyclesPerEtu),
74 15 acapola
                .stopBit2(stopBit2),
75
                .oddParity(oddParity),
76
      .msbFirst(msbFirst),
77
           .dataOut(uart_dataOut),
78 3 acapola
                .nCsDataOut(nCsDataOut),
79
                .statusOut(statusOut),
80
                .nCsStatusOut(nCsStatusOut),
81
                .serialIn(isoSio),
82
                .serialOut(serialOut),
83 4 acapola
                .comClk(cardIsoClk)
84 3 acapola
        );
85
 
86 4 acapola
reg sendAtr;
87
reg [8:0] tsCnt;//counter to start ATR 400 cycles after reset release
88
 
89
reg [7:0] buffer[256+5:0];
90
localparam CLA_I= 8*4;
91
localparam INS_I= 8*3;
92
localparam P1_I = 8*2;
93
localparam P2_I = 8*1;
94
localparam P3_I = 0;
95
reg [CLA_I+7:0] tpduHeader;
96
 
97
wire COM_statusOut=statusOut;
98
wire COM_clk=isoClk;
99
integer COM_errorCnt;
100
 
101 3 acapola
wire txRun,txPending, rxRun, rxStartBit, isTx, overrunErrorFlag, frameErrorFlag, bufferFull;
102
assign {txRun, txPending, rxRun, rxStartBit, isTx, overrunErrorFlag, frameErrorFlag, bufferFull} = statusOut;
103
 
104 4 acapola
`include "ComDriverTasks.v"
105
 
106 3 acapola
assign isoSio = isTx ? serialOut : 1'bz;
107
 
108 4 acapola
 
109
/*T=0 card model
110
 
111
ATR:
112 19 acapola
        3B/3F 90 97 40 20
113 13 acapola
        3B/3F 94 97 80 1F 42 BA BE BA BE
114 14 acapola
        3B 9E 96 80 1F C7 80 31 E0 73 FE 21 1B 66 D0 00 28 24 01 00 0D
115 4 acapola
 
116 14 acapola
 
117 4 acapola
Implemented commands:
118
        write buffer:
119
                tpdu: 00 0C 00 00 LC data
120
                sw:   90 00
121
        read buffer:
122
                tpdu: 00 0A 00 00 LE
123
                response: data
124
                sw:   90 00
125 15 acapola
        toggle communication convention (take effect at next reset):
126
                tpdu 00 FC 00 00 00
127
                sw:     90 00
128 4 acapola
        any other:
129
                sw:   69 86
130
*/
131
task sendAckByte;
132
        sendByte(tpduHeader[INS_I+7:INS_I]);
133
endtask
134
 
135
task writeBufferCmd;
136
integer i;
137
begin
138
        sendAckByte;
139
        for(i=0;i<tpduHeader[P3_I+7:P3_I];i=i+1) begin
140
                receiveByte(buffer[i]);
141
        end
142 9 acapola
        sendHexBytes("9000");//sendWord(16'h9000);
143 4 acapola
end
144
endtask
145
 
146
task readBufferCmd;
147
integer i;
148
integer le;
149
begin
150
        sendAckByte;
151
        le=tpduHeader[P3_I+7:P3_I];
152
        if(0==le) le=256;
153
        for(i=0;i<le;i=i+1) begin
154
                sendByte(buffer[i]);
155
        end
156 9 acapola
        sendHexBytes("9000");//sendWord(16'h9000);
157 4 acapola
end
158
endtask
159
 
160 15 acapola
task toggleConventionCmd;
161 4 acapola
integer i;
162 15 acapola
begin
163
        useIndirectConventionConfig=~useIndirectConventionConfig;
164
        sendHexBytes("9000");//sendWord(16'h9000);
165
end
166
endtask
167
 
168
//stuff which can be changed by command and affect ATR
169
always @(posedge isoVdd) begin
170 17 acapola
        useIndirectConventionConfig<=1'b0;
171 15 acapola
end
172
 
173
integer i;
174 3 acapola
always @(posedge isoClk, negedge isoReset) begin
175
        if(~isoReset) begin
176
                nWeDataIn<=1'b1;
177
                nCsDataOut<=1'b1;
178
                nCsStatusOut<=1'b1;
179
                tsCnt<=9'b0;
180
                sendAtr<=1'b1;
181 7 acapola
                cyclesPerEtu <= 13'd372-1'b1;
182 15 acapola
                useIndirectConvention<=useIndirectConventionConfig;
183 3 acapola
        end else if(tsCnt!=9'd400) begin
184
                tsCnt <= tsCnt + 1'b1;
185
        end else if(sendAtr) begin
186
                sendAtr<=1'b0;
187 13 acapola
                //sendHexBytes("3B00");
188 15 acapola
                if(useIndirectConvention)
189
                        sendHexBytes("3F");
190
                else
191
                        sendHexBytes("3B");
192 19 acapola
                        sendHexBytes("90974020");
193 14 acapola
                //sendHexBytes("9497801F42BABEBABE");
194
                //sendHexBytes("9E 97 80 1F C7 80 31 E0 73 FE 21 1B 66 D0 00 28 24 01 00 0D");
195 19 acapola
                //sendHexBytes("9E 97 80 1F C7 80 31 E0 73 FE 21 1B 66 D0 00 28 24 01 00 ");
196 4 acapola
                waitEndOfTx;
197 3 acapola
        end else begin
198 11 acapola
                //get CLA
199
                receiveByte(tpduHeader[CLA_I+:8]);
200
 
201
                //get INS~P2 or PPS
202
                for(i=1;i<4;i=i+1)
203 4 acapola
                        receiveByte(tpduHeader[(CLA_I-(i*8))+:8]);
204 11 acapola
 
205
                if(8'hFF==tpduHeader[CLA_I+:8]) begin
206
                        //support only PPS8 for the time being
207 12 acapola
                        if(32'hFF109778==tpduHeader[7+CLA_I:P2_I]) begin
208
                                sendHexBytes("FF109778");
209 11 acapola
                                waitEndOfTx;
210
                                cyclesPerEtu <= 13'd8-1'b1;
211
                        end
212
                end else begin
213
                        //tpdu: get P3
214
                        receiveByte(tpduHeader[P3_I+:8]);
215
                        //dispatch
216
                        case(tpduHeader[7+CLA_I:P2_I])
217
                                        32'h000C0000: writeBufferCmd;
218
                                        32'h000A0000: readBufferCmd;
219 15 acapola
                                        32'h00FC0000: toggleConventionCmd;
220
                                        default: sendHexBytes("6986");
221 11 acapola
                        endcase
222
                end
223 3 acapola
        end
224
end
225
 
226
endmodule
227 11 acapola
`default_nettype wire
228 3 acapola
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.