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

Subversion Repositories iso7816_3_master

[/] [iso7816_3_master/] [trunk/] [test/] [iso7816_3_t0_analyzer.v] - Blame information for rev 10

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

Line No. Rev Author Line
1 5 acapola
`timescale 1ns / 1ps
2
`default_nettype none
3
 
4 10 acapola
module Iso7816_3_t0_analyzer
5
#(parameter DIVIDER_WIDTH = 1)
6
(
7 5 acapola
        input wire nReset,
8
        input wire clk,
9 6 acapola
        input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
10 5 acapola
        input wire isoReset,
11
        input wire isoClk,
12
        input wire isoVdd,
13 10 acapola
        input wire isoSioTerm,
14
        input wire isoSioCard,
15
        input wire useDirectionProbe,//if 1, isoSioTerm and isoSioCard must be connected to Iso7816_directionProbe outputs
16 5 acapola
        output reg [3:0] fiCode,
17
        output reg [3:0] diCode,
18 6 acapola
        output wire [12:0] fi,
19
        output wire [7:0] di,
20
        output wire [12:0] cyclesPerEtu,
21
        output wire [7:0] fMax,
22 5 acapola
        output wire isActivated,
23
        output wire tsReceived,
24
        output wire tsError,
25
        output wire useIndirectConvention,
26
        output wire atrIsEarly,//high if TS received before 400 cycles after reset release
27
        output wire atrIsLate,//high if TS is still not received after 40000 cycles after reset release
28 6 acapola
        output reg [3:0] atrK,//number of historical bytes
29
        output reg atrHasTck,
30
        output reg atrCompleted,
31 5 acapola
        output reg useT0,
32
        output reg useT1,
33
        output reg useT15,
34
        output reg waitCardTx,
35
        output reg waitTermTx,
36 10 acapola
        output wire cardTx,
37
        output wire termTx,
38 5 acapola
        output wire guardTime,
39
        output wire overrunError,
40
        output wire frameError,
41 7 acapola
        output reg [7:0] lastByte,
42
        output reg [31:0] bytesCnt
43 5 acapola
        );
44 10 acapola
 
45
wire isoSio = isoSioTerm & isoSioCard;
46 5 acapola
 
47
reg [8:0] tsCnt;//counter to start ATR 400 cycles after reset release
48
 
49
reg [7:0] buffer[256+5:0];
50
localparam CLA_I= 8*4;
51
localparam INS_I= 8*3;
52
localparam P1_I = 8*2;
53
localparam P2_I = 8*1;
54
localparam P3_I = 0;
55
reg [CLA_I+7:0] tpduHeader;
56
 
57 7 acapola
//wire COM_clk=isoClk;
58
//integer COM_errorCnt;
59
//wire txPending=1'b0;
60
//wire txRun=1'b0;
61 5 acapola
 
62
wire rxRun, rxStartBit, overrunErrorFlag, frameErrorFlag, bufferFull;
63
assign overrunErrorFlag = overrunError;
64
assign frameErrorFlag = frameError;
65
 
66
wire [7:0] rxData;
67 7 acapola
reg ackFlags;
68 5 acapola
 
69 6 acapola
wire msbFirst = useIndirectConvention;
70
wire sioHighValue = ~useIndirectConvention;
71
wire oddParity = 1'b0;
72
 
73
wire [7:0] dataOut = sioHighValue ? rxData : ~rxData;
74
 
75
 
76 7 acapola
//`include "ComRxDriverTasks.v"
77 5 acapola
 
78
wire endOfRx;
79
 
80 6 acapola
wire stopBit2 = useT0;//1 if com use 2 stop bits --> 12 ETU / byte
81 10 acapola
wire [12:0] clocksPerBit = cyclesPerEtu-1;
82 5 acapola
RxCoreSelfContained #(
83 6 acapola
                .DIVIDER_WIDTH(DIVIDER_WIDTH),
84 7 acapola
                .CLOCK_PER_BIT_WIDTH(4'd13),
85
                .PRECISE_STOP_BIT(1'b1))
86 5 acapola
        rxCore (
87
    .dataOut(rxData),
88
    .overrunErrorFlag(overrunError),
89
    .dataOutReadyFlag(bufferFull),
90
    .frameErrorFlag(frameError),
91
    .endOfRx(endOfRx),
92
    .run(rxRun),
93
    .startBit(rxStartBit),
94
         .stopBit(guardTime),
95
    .clkPerCycle(clkPerCycle),
96 10 acapola
    .clocksPerBit(clocksPerBit),
97 5 acapola
    .stopBit2(stopBit2),
98
    .oddParity(oddParity),
99
    .msbFirst(msbFirst),
100 7 acapola
         .ackFlags(ackFlags),
101 5 acapola
    .serialIn(isoSio),
102 6 acapola
    .comClk(isoClk),
103 5 acapola
    .clk(clk),
104
    .nReset(nReset)
105
    );
106
 
107
TsAnalyzer tsAnalyzer(
108
        .nReset(nReset),
109
        .isoReset(isoReset),
110
        .isoClk(isoClk),
111
        .isoVdd(isoVdd),
112
        .isoSio(isoSio),
113
        .endOfRx(endOfRx),
114
        .rxData(rxData),
115
        .isActivated(isActivated),
116
        .tsReceived(tsReceived),
117
        .tsError(tsError),
118
        .atrIsEarly(atrIsEarly),
119
        .atrIsLate(atrIsLate),
120
        .useIndirectConvention(useIndirectConvention)
121
        );
122
 
123
FiDiAnalyzer fiDiAnalyzer(
124
        .fiCode(fiCode),
125
        .diCode(diCode),
126
        .fi(fi),
127
        .di(di),
128
        .cyclesPerEtu(cyclesPerEtu),
129
        .fMax(fMax)
130
        );
131
 
132
wire run = rxStartBit | rxRun;
133 6 acapola
localparam ATR_T0 = 0;
134
localparam ATR_TDI = 1;
135
localparam ATR_HISTORICAL = 2;
136
localparam ATR_TCK = 3;
137
localparam T0_HEADER = 0;
138 8 acapola
localparam T0_HEADER_TPDU = 1;
139
localparam T0_PB = 2;
140
localparam T0_DATA = 3;
141
localparam T0_NACK_DATA = 4;
142
localparam T0_SW1 = 5;
143
localparam T0_SW2 = 6;
144
localparam T0_HEADER_PPS = 100;
145
 
146 6 acapola
integer fsmState;
147
 
148
reg [11:0] tdiStruct;
149
wire [3:0] tdiCnt;//i+1
150
wire [7:0] tdiData;//value of TDi
151
assign {tdiCnt,tdiData}=tdiStruct;
152
 
153
wire [1:0] nIfBytes;
154
HammingWeight hammingWeight(.dataIn(tdiData[7:4]), .hammingWeight(nIfBytes));
155 7 acapola
reg [7:0] tempBytesCnt;
156 6 acapola
always @(posedge isoClk, negedge nReset) begin
157 5 acapola
        if(~nReset) begin
158 7 acapola
                lastByte<=8'b0;
159
                ackFlags<=1'b0;
160
                bytesCnt<=32'b0;
161
        end else if(ackFlags) begin
162
                ackFlags<=1'b0;
163
        end else if(frameErrorFlag|bufferFull) begin
164
                lastByte<=dataOut;
165
                ackFlags<=1'b1;
166
                bytesCnt<=bytesCnt+1'b1;
167
        end
168
end
169
always @(posedge isoClk, negedge nReset) begin
170
        if(~nReset) begin
171 5 acapola
                fiCode<=4'b0001;
172
                diCode<=4'b0001;
173 6 acapola
                useT0<=1'b1;
174 5 acapola
                useT1<=1'b0;
175
                useT15<=1'b0;
176 8 acapola
                {waitCardTx,waitTermTx}<=2'b00;
177 6 acapola
                fsmState<=ATR_TDI;
178
                atrHasTck<=1'b0;
179 7 acapola
                tempBytesCnt<=8'h0;
180 6 acapola
                tdiStruct<=12'h0;
181
                atrCompleted<=1'b0;
182 8 acapola
                atrK<=4'b0;
183 5 acapola
        end else if(isActivated) begin
184
                if(~tsReceived) begin
185 8 acapola
                        {waitCardTx,waitTermTx}<=2'b10;
186 5 acapola
                end else if(~atrCompleted) begin
187 6 acapola
                        //ATR analysis
188
                        case(fsmState)
189
                                ATR_TDI: begin
190
                                        if(endOfRx) begin
191 7 acapola
                                                if(tempBytesCnt==nIfBytes) begin //TDi bytes
192
                                                        tempBytesCnt <= 2'h0;
193 6 acapola
                                                        tdiStruct <= {tdiCnt+1,dataOut};
194
                                                        if(4'h0==tdiCnt) begin//this is T0
195
                                                                atrK <= dataOut[3:0];
196
                                                                fsmState <= (4'b0!=dataOut[7:4]) ? ATR_TDI :
197
                                                                                                (4'b0!=dataOut[3:0]) ? ATR_HISTORICAL : T0_HEADER;
198
                                                        end else begin//TDi, i from 1 to 15
199
                                                                fsmState <= (4'b0!=dataOut[7:4]) ? ATR_TDI :
200
                                                                                                (4'b0!=atrK) ? ATR_HISTORICAL : T0_HEADER;
201
                                                        end
202 8 acapola
                                                        if(12'h0=={dataOut,atrK}) begin
203
                                                                atrCompleted <= 1'b1;
204
                                                                {waitCardTx,waitTermTx}<=2'b01;
205
                                                        end
206 6 acapola
                                                end else begin //TA, TB or TC bytes
207
                                                        //TODO: get relevant info
208 7 acapola
                                                        tempBytesCnt <= tempBytesCnt+1;
209 6 acapola
                                                end
210
                                        end
211
                                end
212
                                ATR_HISTORICAL: begin
213
                                        if(endOfRx) begin
214 7 acapola
                                                if(tempBytesCnt==atrK) begin
215 8 acapola
                                                        tempBytesCnt <= 8'h0;
216
                                                        if(atrHasTck) begin
217
                                                                fsmState <= ATR_TCK;
218
                                                        end else begin
219
                                                                atrCompleted <= ~atrHasTck;
220
                                                                {waitCardTx,waitTermTx}<=2'b10;
221
                                                                fsmState <= T0_HEADER;
222
                                                        end
223 6 acapola
                                                end else begin
224 7 acapola
                                                        tempBytesCnt <= tempBytesCnt+1;
225 6 acapola
                                                end
226
                                        end
227
                                end
228
                                ATR_TCK: begin
229
                                        if(endOfRx) begin
230
                                        //TODO:check
231
                                                atrCompleted <= 1'b1;
232 8 acapola
                                                {waitCardTx,waitTermTx}<=2'b10;
233 6 acapola
                                                fsmState <= T0_HEADER;
234
                                        end
235
                                end
236
                        endcase
237 5 acapola
                end else if(useT0) begin
238
                        //T=0 cmd/response monitoring state machine
239 8 acapola
                        case(fsmState)
240
                                T0_HEADER: begin
241
                                        if(endOfRx) begin
242
                                                tpduHeader[CLA_I+:8]<=dataOut;
243
                                                tempBytesCnt <= 1;
244
                                                if(8'hFF==dataOut)
245
                                                        fsmState <= T0_HEADER_PPS;//TODO
246
                                                else
247
                                                        fsmState <= T0_HEADER_TPDU;
248
                                        end
249
                                end
250
                                T0_HEADER_TPDU: begin
251
                                        if(endOfRx) begin
252
                                                tpduHeader[(CLA_I-(tempBytesCnt*8))+:8]<=dataOut;
253
                                                if(4==tempBytesCnt) begin
254
                                                        tempBytesCnt <= 8'h0;
255
                                                        fsmState <= T0_PB;
256
                                                        {waitCardTx,waitTermTx}<=2'b10;
257
                                                end else begin
258
                                                        tempBytesCnt <= tempBytesCnt+1;
259
                                                end
260
                                        end
261
                                end
262
                                T0_PB: begin
263
                                        if(endOfRx) begin
264
                                                case(dataOut[7:4])
265
                                                        4'h6: begin
266
                                                                fsmState <= (4'h0==dataOut[3:0]) ? T0_PB : T0_SW2;
267
                                                        end
268
                                                        4'h9: begin
269
                                                                fsmState <= T0_SW2;
270
                                                        end
271
                                                        default: begin
272
                                                                case(dataOut)
273
                                                                        tpduHeader[INS_I+:8]: begin//ACK
274
                                                                                fsmState <= T0_DATA;
275
                                                                                {waitCardTx,waitTermTx}<=2'b11;
276
                                                                        end
277
                                                                        ~tpduHeader[INS_I+:8]: begin//NACK
278
                                                                                fsmState <= T0_NACK_DATA;
279
                                                                                {waitCardTx,waitTermTx}<=2'b11;
280
                                                                        end
281
                                                                        default: begin //invalid
282
                                                                                //TODO
283
                                                                        end
284
                                                                endcase
285
                                                        end
286
                                                endcase
287
                                        end
288
                                end
289
                                T0_NACK_DATA: begin
290
                                        if(endOfRx) begin
291
                                                fsmState <= T0_PB;
292
                                                {waitCardTx,waitTermTx}<=2'b10;
293
                                                tempBytesCnt <= tempBytesCnt+1;
294
                                        end
295
                                end
296
                                T0_SW1: begin
297
                                        if(endOfRx) begin
298
                                        //TODO:check != 60 but equal to 6x or 9x
299
                                                fsmState <= T0_SW2;
300
                                                {waitCardTx,waitTermTx}<=2'b10;
301
                                        end
302
                                end
303
                                T0_SW2: begin
304
                                        if(endOfRx) begin
305
                                                fsmState <= T0_HEADER;
306
                                                {waitCardTx,waitTermTx}<=2'b01;
307
                                        end
308
                                end
309
                                T0_DATA: begin
310
                                        if(endOfRx) begin
311
                                                if(tempBytesCnt==(tpduHeader[P3_I+:8]-1)) begin
312
                                                        tempBytesCnt <= 0;
313
                                                        fsmState <= T0_SW1;
314
                                                        {waitCardTx,waitTermTx}<=2'b10;
315
                                                end else begin
316
                                                        tempBytesCnt <= tempBytesCnt+1;
317
                                                end
318
                                        end
319
                                end
320
                        endcase
321 5 acapola
                end
322
        end
323
end
324
 
325
reg [1:0] txDir;
326 10 acapola
reg proto_cardTx;
327
reg proto_termTx;
328
always @(*) begin: protoComDirectionCombiBlock
329 6 acapola
        if(guardTime & ~isoSio)
330 10 acapola
                {proto_cardTx, proto_termTx}={txDir[0],txDir[1]};
331 5 acapola
        else
332 10 acapola
                {proto_cardTx, proto_termTx}={txDir[1],txDir[0]};
333 5 acapola
end
334 10 acapola
always @(posedge isoClk, negedge nReset) begin: protoComDirectionSeqBlock
335 5 acapola
        if(~nReset | ~run) begin
336
                txDir<=2'b00;
337
        end else begin
338 6 acapola
                if(~guardTime) begin //{waitCardTx, waitTermTx} is updated during stop bits so we hold current value here
339 5 acapola
                        case({waitCardTx, waitTermTx})
340 8 acapola
                                2'b00: txDir<=2'b00;//no one should/is sending
341
                                2'b01: txDir<=2'b01;//terminal should/is sending
342
                                2'b10: txDir<=2'b10;//card should/is sending
343
                                2'b11: txDir<=2'b11;//either card OR terminal should/is sending (we just don't know)
344 5 acapola
                        endcase
345
                end
346
        end
347
end
348 10 acapola
 
349
reg phy_cardTx;
350
reg phy_termTx;
351
always @(negedge isoSio, negedge nReset) begin: phyComDirectionBlock
352
        if(~nReset) begin
353
                phy_cardTx<=1'b0;
354
                phy_termTx<=1'b0;
355
        end else begin
356
                if(~isoSioTerm) begin
357
                        phy_cardTx<=1'b0;
358
                        phy_termTx<=1'b1;
359
                end else begin
360
                        phy_cardTx<=1'b1;
361
                        phy_termTx<=1'b0;
362
                end
363
        end
364
end
365
 
366
assign cardTx = useDirectionProbe ? phy_cardTx : proto_cardTx;
367
assign termTx = useDirectionProbe ? phy_termTx : proto_termTx;
368 5 acapola
 
369
endmodule
370
 

powered by: WebSVN 2.1.0

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