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 5

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

powered by: WebSVN 2.1.0

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