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

Subversion Repositories iso7816_3_master

[/] [iso7816_3_master/] [trunk/] [sources/] [RxCore.v] - Blame information for rev 10

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

Line No. Rev Author Line
1 4 acapola
`timescale 1ns / 1ps
2
`default_nettype none
3 2 acapola
//////////////////////////////////////////////////////////////////////////////////
4
// Company: 
5
// Engineer: Sebastien Riou
6
// 
7
// Create Date:    23:57:02 08/31/2010 
8
// Design Name: 
9
// Module Name:    RxCore 
10
// Project Name: 
11
// Target Devices: 
12
// Tool versions: 
13
// Description: 
14
//
15
// Dependencies: 
16
//
17
// Revision: 
18
// Revision 0.01 - File Created
19
// Additional Comments: 
20
//
21
//////////////////////////////////////////////////////////////////////////////////
22
module RxCore(
23
   output reg [7:0] dataOut,
24
   output reg overrunErrorFlag, //new data has been received before dataOut was read
25
   output reg dataOutReadyFlag, //new data available
26
   output reg frameErrorFlag,           //bad parity or bad stop bits
27
   output reg endOfRx,                          //one cycle pulse: 1 during last cycle of last stop bit
28
   output reg run,                                      //rx is definitely started, one of the three flag will be set
29 5 acapola
   output wire startBit,                        //rx is started, but we don't know yet if real rx or just a glitch
30 7 acapola
        output reg stopBit,                             //rx is over but still in stop bits
31 4 acapola
        input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
32
        input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
33
        input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
34
   input wire msbFirst,  //if 1, bits order is: startBit, b7, b6, b5...b0, parity
35
        input wire ackFlags,
36
        input wire serialIn,
37
        input wire clk,
38
   input wire nReset,
39 2 acapola
        //to connect to an instance of Counter.v (see RxCoreSelfContained.v for example)
40
        output reg [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounterCompare,
41
        output reg bitClocksCounterInc,
42
        output reg bitClocksCounterClear,
43 4 acapola
        output wire bitClocksCounterInitVal,
44
   input wire bitClocksCounterEarlyMatch,
45 7 acapola
        input wire bitClocksCounterMatch,
46
        input wire [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounter
47 2 acapola
    );
48
 
49
//parameters to override
50
parameter CLOCK_PER_BIT_WIDTH = 13;     //allow to support default speed of ISO7816
51 7 acapola
parameter PRECISE_STOP_BIT = 0; //if 1, stopBit signal goes high exactly at start of stop bit instead of middle of parity bit
52 5 acapola
 
53 2 acapola
//default conventions
54
parameter START_BIT = 1'b0;
55
parameter STOP_BIT1 = 1'b1;
56
parameter STOP_BIT2 = 1'b1;
57
 
58
//constant definition for states
59
localparam IDLE_STATE =         3'b000;
60
localparam START_STATE =        3'b001;
61
localparam DATA_STATE =         3'b011;
62
localparam PARITY_STATE =       3'b010;
63
localparam STOP1_STATE =        3'b110;
64
localparam STOP2_STATE =        3'b111;
65
localparam END_STATE =          3'b101;
66
localparam END2_STATE =    3'b100;
67
 
68
localparam IDLE_BIT = ~START_BIT;
69
 
70
reg [2:0] nextState;
71
 
72
reg [2:0] bitCounter;
73
wire [2:0] bitIndex = msbFirst ? 7-bitCounter : bitCounter;
74
reg parityBit;
75
 
76
wire internalIn;
77
wire parityError;
78
 
79 5 acapola
assign startBit = (nextState == START_STATE);
80 7 acapola
//assign stopBit = (nextState == STOP1_STATE) | (nextState == STOP2_STATE);
81 2 acapola
assign internalIn = serialIn;
82
assign parityError= parityBit ^ internalIn ^ 1'b1;
83
reg flagsSet;
84
 
85
assign bitClocksCounterInitVal=(nextState==IDLE_STATE);
86
always @(nextState, clocksPerBit, run, bitClocksCounterMatch) begin
87
        case(nextState)
88
                IDLE_STATE: begin
89
                        bitClocksCounterCompare = (clocksPerBit/2);
90
                        bitClocksCounterInc = run & ~bitClocksCounterMatch;//stop when reach 0
91
                        bitClocksCounterClear = ~run;
92
                end
93
                START_STATE: begin
94
                        bitClocksCounterCompare = (clocksPerBit/2);
95
                        bitClocksCounterInc = 1;
96
                        bitClocksCounterClear = 0;
97
                end
98
                STOP2_STATE: begin
99
         //make the rx operation is one cycle shorter, 
100
         //since we detect the start bit at least one cycle later it starts.
101
                        bitClocksCounterCompare = clocksPerBit-1;
102
                        bitClocksCounterInc = 1;
103
                        bitClocksCounterClear = 0;
104
                end
105
                default: begin
106
                        bitClocksCounterCompare = clocksPerBit;
107
                        bitClocksCounterInc = 1;
108
                        bitClocksCounterClear = 0;
109
                end
110
        endcase
111
end
112
 
113
always @(posedge clk, negedge nReset) begin
114
        if(~nReset) begin
115
                nextState <= #1 IDLE_STATE;
116
                bitCounter <= #1 0;
117
                parityBit <= #1 0;
118
                overrunErrorFlag <= #1 0;
119
                dataOutReadyFlag <= #1 0;
120
                frameErrorFlag <= #1 0;
121
                run <= #1 0;
122 7 acapola
      endOfRx <= #1 0;
123
                stopBit<= #1 0;
124 2 acapola
        end else begin
125
                case(nextState)
126
                        IDLE_STATE: begin
127
                                if(bitClocksCounterEarlyMatch)
128
               endOfRx <= #1 1'b1;
129 7 acapola
            if(bitClocksCounterMatch) begin
130
               endOfRx <= #1 0;
131
                                        stopBit <= #1 0;
132
                                end
133 2 acapola
            if(ackFlags) begin
134
                                        //overrunErrorFlag is auto cleared at PARITY_STATE
135
                                        //meanwhile, it prevent dataOutReadyFlag to be set by the termination of the lost byte
136
                                        dataOutReadyFlag <= #1 0;
137
                                        frameErrorFlag <= #1 0;
138
                                end
139
                                if(START_BIT == internalIn) begin
140
                                        if(frameErrorFlag | overrunErrorFlag) begin
141
                                                //wait clear from outside
142
                                                if(bitClocksCounterMatch) begin
143
                     //endOfRx <= #1 0;
144
                                                        run <= #1 0;
145
                  end
146
                                        end else begin
147 9 acapola
                                                parityBit <= #1 ~oddParity;
148 2 acapola
                                                run <= #1 0;
149
                                                nextState <= #1 START_STATE;
150
                                        end
151
                                end else begin
152
                                        if(bitClocksCounterMatch) begin
153
                  //endOfRx <= #1 0;
154
                                                run <= #1 0;
155
               end
156
                                end
157
                        end
158
                        START_STATE: begin
159
                                if(ackFlags) begin
160
                                        dataOutReadyFlag <= #1 0;
161
                                        frameErrorFlag <= #1 0;
162
                                end
163
                                if(bitClocksCounterMatch) begin
164
                                        if(START_BIT != internalIn) begin
165
                                                nextState <= #1 IDLE_STATE;
166
                                        end else begin
167
                                                run <= #1 1;
168
                                                nextState <= #1 DATA_STATE;
169
                                        end
170
                                end
171
                        end
172
                        DATA_STATE: begin
173
                                if(ackFlags) begin
174
                                        dataOutReadyFlag <= #1 0;
175
                                        frameErrorFlag <= #1 0;
176
                                end
177
                                if(bitClocksCounterMatch) begin
178
                                        if(dataOutReadyFlag) begin
179
                                                overrunErrorFlag <= #1 1;
180
                                        end else
181
                                                dataOut[bitIndex] <= #1 internalIn;
182
                                        parityBit <= #1 parityBit ^ internalIn;
183
                                        bitCounter <= #1 (bitCounter + 1'b1) & 3'b111;
184
                                        if(bitCounter == 7)
185
                                                nextState <= #1 PARITY_STATE;
186
                                end
187
                        end
188
                        PARITY_STATE: begin
189
                                if(bitClocksCounterMatch) begin
190
                                        if(~overrunErrorFlag) begin
191
                                                frameErrorFlag <= #1 parityError;
192
                                                dataOutReadyFlag <= #1 ~parityError;
193
                                        end else if(ackFlags) begin
194
                                                frameErrorFlag <= #1 0;
195
                                        end
196 7 acapola
                                        flagsSet=1;
197
                                        if(PRECISE_STOP_BIT==0) stopBit <= #1 1;
198 2 acapola
                                        if(stopBit2)
199
                                                nextState <= #1 STOP1_STATE;
200
                                        else
201
                                                nextState <= #1 STOP2_STATE;
202
                                end else if(ackFlags) begin
203
                                        dataOutReadyFlag <= #1 0;
204
                                        frameErrorFlag <= #1 0;
205
                                end
206
                        end
207
                        STOP1_STATE: begin
208
                                if(ackFlags) begin
209
                                        dataOutReadyFlag <= #1 0;
210
                                end
211
                                if(bitClocksCounterMatch) begin
212
                                        if(STOP_BIT1 != internalIn) begin
213
                                                frameErrorFlag <= #1 parityError;
214
                                        end else if(ackFlags) begin
215
                                                frameErrorFlag <= #1 0;
216
                                        end
217
                                        nextState <= #1 STOP2_STATE;
218
                                end else if(ackFlags) begin
219
                                        frameErrorFlag <= #1 0;
220 7 acapola
                                end
221
                                if(PRECISE_STOP_BIT!=0) begin
222
                                        if(bitClocksCounter==(bitClocksCounterCompare/2)) begin
223
                                                stopBit <= #1 1;
224
                                        end
225 2 acapola
                                end
226
                        end
227
                        STOP2_STATE: begin
228
                                if(ackFlags) begin
229
                                        dataOutReadyFlag <= #1 0;
230
                                end
231
            if(bitClocksCounterMatch) begin
232
                                        if(STOP_BIT2 != internalIn) begin
233
                                                frameErrorFlag <= #1 1;
234
                                        end else if(ackFlags) begin
235
                                                frameErrorFlag <= #1 0;
236
                                        end
237
                                        nextState <= #1 IDLE_STATE;
238
                                end else if(ackFlags) begin
239
                                        frameErrorFlag <= #1 0;
240 7 acapola
                                end
241
                                if(PRECISE_STOP_BIT!=0) begin
242
                                        if(bitClocksCounter==(bitClocksCounterCompare/2)) begin
243
                                                stopBit <= #1 1;
244
                                        end
245 2 acapola
                                end
246
                        end
247
                        default: nextState <= #1 IDLE_STATE;
248
                endcase
249
        end
250
end
251
 
252
endmodule

powered by: WebSVN 2.1.0

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