-
Notifications
You must be signed in to change notification settings - Fork 3
/
TX.v
75 lines (68 loc) · 1.8 KB
/
TX.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module TX(dataIn, length, clk, reset, request, dataOut, ready);
//// Necessary parameters
parameter LENGTH_NUMOF_BIT = 12;
parameter STATE_NUMOF_BIT = 2;
parameter IDLE = 2'b00, INITIALIZE = 2'b01, SCRAMBLING = 2'b10, FINISH = 2'b11;
////
//// Input output declaration
input clk, request, dataIn, reset;
input [LENGTH_NUMOF_BIT-1:0] length;
output reg ready;
output dataOut;
////
//// FSM setup
// Necessary reg and wire declaration
reg [STATE_NUMOF_BIT-1:0] currentState;
reg [STATE_NUMOF_BIT-1:0] nextState;
wire scramblingEnd;
reg [LENGTH_NUMOF_BIT-1:0] counter;
// State transition
always @(currentState,scramblingEnd,request)
begin
case(currentState)
IDLE: if(request==1'b1) nextState = INITIALIZE;
else nextState = IDLE;
INITIALIZE: nextState = SCRAMBLING;
SCRAMBLING: if(scramblingEnd==1'b1)nextState = FINISH;
else nextState = SCRAMBLING;
FINISH: nextState = IDLE;
endcase
end
// State Assignment
always @(posedge clk)
begin
if(reset==1'b1) currentState = IDLE;
else currentState = nextState;
end
////
//// Floping the length
reg [LENGTH_NUMOF_BIT-1:0]lengthBuffer;
always @(posedge clk) begin
if(request==1'b1)lengthBuffer <= length;
end
////
//// Counter setup
assign scramblingEnd = (counter == lengthBuffer);
always @(posedge clk) begin
if(currentState==INITIALIZE | currentState==IDLE | currentState==FINISH) counter <= 0;
else counter <= counter + 1;
end
////
//// ready signal assignment
reg bitToScramble;
always @(posedge clk) begin
if(currentState==SCRAMBLING)begin
ready=1'b1;
bitToScramble = dataIn;
end
else ready=1'b0;
end
////
////Scrambling Phase
wire [6:0] state_out;
wire scramblingReset;
// Instantiation
assign scramblingReset = (currentState==INITIALIZE);
Scrambler scrambler(clk, scramblingReset, state_out, bitToScramble, dataOut);
////
endmodule