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 2

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

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

powered by: WebSVN 2.1.0

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