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 12

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

Line No. Rev Author Line
1 11 acapola
/*
2
Author: Sebastien Riou (acapola)
3
Creation date: 21:16:10 08/29/2010
4
 
5
$LastChangedDate: 2011-01-29 17:13:49 +0100 (Sat, 29 Jan 2011) $
6
$LastChangedBy: acapola $
7
$LastChangedRevision: 12 $
8
$HeadURL: file:///svn/iso7816_3_master/iso7816_3_master/trunk/sources/TxCore.v $
9
 
10
This file is under the BSD licence:
11
Copyright (c) 2011, Sebastien Riou
12
 
13
All rights reserved.
14
 
15
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
16
 
17
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
18
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
19
The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
`default_nettype none
33 4 acapola
`timescale 1ns / 1ps
34 11 acapola
 
35
module TxCore
36
#(//parameters to override
37
        parameter DIVIDER_WIDTH = 1,
38
        parameter CLOCK_PER_BIT_WIDTH = 13,//allow to support default speed of ISO7816
39
        //default conventions
40
        parameter START_BIT = 1'b0,
41
        parameter STOP_BIT1 = 1'b1
42
)
43
(
44 4 acapola
    output wire comClk,
45
    output wire serialOut,
46 12 acapola
    output wire run,
47
         output reg endOfTx,
48 4 acapola
    output wire full,
49
    output wire stopBits, //1 during stop bits
50
    input wire [7:0] dataIn,
51
    input wire [DIVIDER_WIDTH-1:0] clkPerCycle,
52
         input wire [CLOCK_PER_BIT_WIDTH-1:0] clocksPerBit,
53
         input wire loadDataIn,   //evaluated only when full=0, when full goes to one, dataIn has been read
54
    input wire stopBit2,//0: 1 stop bit, 1: 2 stop bits
55
    input wire oddParity, //if 1, parity bit is such that data+parity have an odd number of 1
56
    input wire msbFirst,  //if 1, bits will be send in the order startBit, b7, b6, b5...b0, parity
57
         input wire clk,
58
    input wire nReset
59 2 acapola
    );
60
 
61
//constant definition for state
62
localparam IDLE_STATE = 0;
63
localparam START_STATE = 1;
64
localparam SEND_DATA_STATE = 2;
65
localparam SEND_PARITY_STATE = 3;
66
localparam SEND_STOP1_STATE = 4;
67
localparam SEND_STOP2_STATE = 5;
68
 
69
localparam IDLE_BIT = ~START_BIT;
70
localparam STOP_BIT2 = STOP_BIT1;
71
 
72
wire [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounter;
73
wire bitClocksCounterEarlyMatch;
74
wire bitClocksCounterMatch;
75
reg [CLOCK_PER_BIT_WIDTH-1:0] bitClocksCounterCompare;
76
reg bitClocksCounterInc;
77
reg bitClocksCounterClear;
78
wire bitClocksCounterInitVal;
79
Counter #(      .DIVIDER_WIDTH(DIVIDER_WIDTH),
80
                                .WIDTH(CLOCK_PER_BIT_WIDTH),
81
                                .WIDTH_INIT(1))
82
                bitClocksCounterModule(
83
                                .counter(bitClocksCounter),
84
                                .earlyMatch(bitClocksCounterEarlyMatch),
85
                                .match(bitClocksCounterMatch),
86
                                .dividedClk(comClk),
87
                                .divider(clkPerCycle),
88
                                .compare(bitClocksCounterCompare),
89
                                .inc(bitClocksCounterInc),
90
                                .clear(bitClocksCounterClear),
91
                                .initVal(bitClocksCounterInitVal),
92
                                .clk(clk),
93
                                .nReset(nReset));
94
 
95
reg [2:0] nextState;
96
reg [2:0] bitCounter;
97
reg [7:0] dataBuffer;
98
 
99
reg parityBit;
100
 
101
wire internalOut;
102
wire dataBit;
103
//after a tx operation, during the first cycle in IDLE_STATE, run bit must be still set 
104
//(it is entered one cycle before the completion of the operation, so we use bitClocksCounter[0]
105
//to implement this behavior)
106
assign run = (nextState == IDLE_STATE) ? bitClocksCounter[0] : 1'b1;
107
assign full = (nextState != IDLE_STATE);
108
assign stopBits = (nextState == SEND_STOP1_STATE)|(nextState == SEND_STOP2_STATE)|((nextState == IDLE_STATE) & bitClocksCounter[0]);
109
 
110
assign serialOut = internalOut;
111
wire [2:0] bitIndex = msbFirst ? 7-bitCounter : bitCounter;
112
assign dataBit = dataBuffer[bitIndex];
113
wire [0:5] bitSel;
114
assign bitSel = {IDLE_BIT, START_BIT, dataBit, parityBit, STOP_BIT1, STOP_BIT2};
115
assign internalOut = bitSel[nextState];
116
 
117
assign bitClocksCounterInitVal=0;
118
 
119
always @(nextState) begin
120
   case(nextState)
121
      START_STATE:
122
         assign bitClocksCounterCompare = clocksPerBit-1;
123
      SEND_STOP2_STATE:
124
         assign bitClocksCounterCompare = clocksPerBit-1;
125
      default:
126
         assign bitClocksCounterCompare = clocksPerBit;
127
   endcase
128
end
129
 
130
always @(nextState) begin
131
        case(nextState)
132
                IDLE_STATE: begin
133
                        bitClocksCounterInc = 0;
134
                        bitClocksCounterClear = 1;
135
                end
136
                default: begin
137
                        bitClocksCounterInc = 1;
138
                        bitClocksCounterClear = 0;
139
                end
140
        endcase
141
end
142
 
143
always @(posedge clk, negedge nReset) begin
144
        if(~nReset) begin
145
                nextState <= #1 IDLE_STATE;
146 12 acapola
                bitCounter <= #1 0;
147
                endOfTx <= #1 1'b0;
148 2 acapola
        end else begin
149
                case(nextState)
150
                        IDLE_STATE: begin
151
                                if(loadDataIn) begin
152
                                        dataBuffer <= #1 dataIn;
153
                                        parityBit <= #1 oddParity;
154
                                        nextState <= #1 START_STATE;
155
                                end
156
                        end
157
                        START_STATE: begin
158
                                if(bitClocksCounterMatch) begin
159
                                        nextState <= #1 SEND_DATA_STATE;
160
                                end
161
                        end
162
                        SEND_DATA_STATE: begin
163
                                if(bitClocksCounterMatch) begin
164
                                        bitCounter <= #1 (bitCounter + 1'b1) & 3'b111;
165
                                        parityBit <= #1 parityBit ^ dataBit;
166
                                        if(bitCounter == 7)
167
                                                nextState <= #1 SEND_PARITY_STATE;
168
                                end
169
                        end
170
                        SEND_PARITY_STATE: begin
171
                                if(bitClocksCounterMatch) begin
172
                                        if(stopBit2)
173
                                                nextState <= #1 SEND_STOP1_STATE;
174
                                        else
175
                                                nextState <= #1 SEND_STOP2_STATE;//if single stop bit, we skip STOP1 state
176
                                end
177
                        end
178
                        SEND_STOP1_STATE: begin
179
                                if(bitClocksCounterMatch)
180
                                        nextState <= #1 SEND_STOP2_STATE;
181
                        end
182
                        SEND_STOP2_STATE: begin
183 12 acapola
                                if(bitClocksCounterEarlyMatch)
184
                                        endOfTx <= #1 1'b1;
185
                                if(bitClocksCounterMatch) begin
186 2 acapola
               nextState <= #1 IDLE_STATE;
187 12 acapola
                                        endOfTx <= #1 1'b0;
188
                                end
189 2 acapola
         end
190
                        default: nextState <= #1 IDLE_STATE;
191
                endcase
192
        end
193
 
194
end
195
 
196
endmodule
197 11 acapola
`default_nettype wire

powered by: WebSVN 2.1.0

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