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 11

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