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

Subversion Repositories iso7816_3_master

[/] [iso7816_3_master/] [trunk/] [sources/] [TxCore.v] - Blame information for rev 3

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:    21:16:10 08/29/2010 
7
// Design Name: 
8
// Module Name:    TxCore 
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 TxCore(
22
    output comClk,
23
    output serialOut,
24
    output run,
25
    output full,
26
    output stopBits, //1 during stop bits
27
    input [7:0] dataIn,
28
    input [DIVIDER_WIDTH-1:0] clkPerCycle,
29
         input [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
30
         input loadDataIn,   //evaluated only when full=0, when full goes to one, dataIn has been read
31
    input stopBit2,//0: 1 stop bit, 1: 2 stop bits
32
    input oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
33
    input msbFirst,  //if 1, bits will be send in the order startBit, b7, b6, b5...b0, parity
34
         input clk,
35
    input nReset
36
    );
37
 
38
//parameters to override
39
parameter DIVIDER_WIDTH = 1;
40
parameter CLOCK_PER_BIT_WIDTH = 13;//allow to support default speed of ISO7816
41
//default conventions
42
parameter START_BIT = 1'b0;
43
parameter STOP_BIT1 = 1'b1;
44
 
45
//constant definition for state
46
localparam IDLE_STATE = 0;
47
localparam START_STATE = 1;
48
localparam SEND_DATA_STATE = 2;
49
localparam SEND_PARITY_STATE = 3;
50
localparam SEND_STOP1_STATE = 4;
51
localparam SEND_STOP2_STATE = 5;
52
 
53
localparam IDLE_BIT = ~START_BIT;
54
localparam STOP_BIT2 = STOP_BIT1;
55
 
56
wire [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounter;
57
wire bitClocksCounterEarlyMatch;
58
wire bitClocksCounterMatch;
59
reg [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounterCompare;
60
reg bitClocksCounterInc;
61
reg bitClocksCounterClear;
62
wire bitClocksCounterInitVal;
63
Counter #(      .DIVIDER_WIDTH(DIVIDER_WIDTH),
64
                                .WIDTH(CLOCK_PER_BIT_WIDTH),
65
                                .WIDTH_INIT(1))
66
                bitClocksCounterModule(
67
                                .counter(bitClocksCounter),
68
                                .earlyMatch(bitClocksCounterEarlyMatch),
69
                                .match(bitClocksCounterMatch),
70
                                .dividedClk(comClk),
71
                                .divider(clkPerCycle),
72
                                .compare(bitClocksCounterCompare),
73
                                .inc(bitClocksCounterInc),
74
                                .clear(bitClocksCounterClear),
75
                                .initVal(bitClocksCounterInitVal),
76
                                .clk(clk),
77
                                .nReset(nReset));
78
 
79
reg [2:0] nextState;
80
reg [2:0] bitCounter;
81
reg [7:0] dataBuffer;
82
 
83
reg parityBit;
84
 
85
wire internalOut;
86
wire dataBit;
87
//after a tx operation, during the first cycle in IDLE_STATE, run bit must be still set 
88
//(it is entered one cycle before the completion of the operation, so we use bitClocksCounter[0]
89
//to implement this behavior)
90
assign run = (nextState == IDLE_STATE) ? bitClocksCounter[0] : 1'b1;
91
assign full = (nextState != IDLE_STATE);
92
assign stopBits = (nextState == SEND_STOP1_STATE)|(nextState == SEND_STOP2_STATE)|((nextState == IDLE_STATE) & bitClocksCounter[0]);
93
 
94
assign serialOut = internalOut;
95
wire [2:0] bitIndex = msbFirst ? 7-bitCounter : bitCounter;
96
assign dataBit = dataBuffer[bitIndex];
97
wire [0:5] bitSel;
98
assign bitSel = {IDLE_BIT, START_BIT, dataBit, parityBit, STOP_BIT1, STOP_BIT2};
99
assign internalOut = bitSel[nextState];
100
 
101
assign bitClocksCounterInitVal=0;
102
 
103
always @(nextState) begin
104
   case(nextState)
105
      START_STATE:
106
         assign bitClocksCounterCompare = clocksPerBit-1;
107
      SEND_STOP2_STATE:
108
         assign bitClocksCounterCompare = clocksPerBit-1;
109
      default:
110
         assign bitClocksCounterCompare = clocksPerBit;
111
   endcase
112
end
113
 
114
always @(nextState) begin
115
        case(nextState)
116
                IDLE_STATE: begin
117
                        bitClocksCounterInc = 0;
118
                        bitClocksCounterClear = 1;
119
                end
120
                default: begin
121
                        bitClocksCounterInc = 1;
122
                        bitClocksCounterClear = 0;
123
                end
124
        endcase
125
end
126
 
127
always @(posedge clk, negedge nReset) begin
128
        if(~nReset) begin
129
                nextState <= #1 IDLE_STATE;
130
                bitCounter <= #1 0;
131
        end else begin
132
                case(nextState)
133
                        IDLE_STATE: begin
134
                                if(loadDataIn) begin
135
                                        dataBuffer <= #1 dataIn;
136
                                        parityBit <= #1 oddParity;
137
                                        nextState <= #1 START_STATE;
138
                                end
139
                        end
140
                        START_STATE: begin
141
                                if(bitClocksCounterMatch) begin
142
                                        nextState <= #1 SEND_DATA_STATE;
143
                                end
144
                        end
145
                        SEND_DATA_STATE: begin
146
                                if(bitClocksCounterMatch) begin
147
                                        bitCounter <= #1 (bitCounter + 1'b1) & 3'b111;
148
                                        parityBit <= #1 parityBit ^ dataBit;
149
                                        if(bitCounter == 7)
150
                                                nextState <= #1 SEND_PARITY_STATE;
151
                                end
152
                        end
153
                        SEND_PARITY_STATE: begin
154
                                if(bitClocksCounterMatch) begin
155
                                        if(stopBit2)
156
                                                nextState <= #1 SEND_STOP1_STATE;
157
                                        else
158
                                                nextState <= #1 SEND_STOP2_STATE;//if single stop bit, we skip STOP1 state
159
                                end
160
                        end
161
                        SEND_STOP1_STATE: begin
162
                                if(bitClocksCounterMatch)
163
                                        nextState <= #1 SEND_STOP2_STATE;
164
                        end
165
                        SEND_STOP2_STATE: begin
166
                        /*      if(bitClocksCounter[1:0]==2'b10)
167
               nextState <= #1 SEND_STOP2_STATE2;
168
                        end
169
                        SEND_STOP2_STATE2: begin*/
170
                                if(bitClocksCounterMatch)
171
               nextState <= #1 IDLE_STATE;
172
         end
173
                        default: nextState <= #1 IDLE_STATE;
174
                endcase
175
        end
176
 
177
end
178
 
179
endmodule

powered by: WebSVN 2.1.0

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