-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOP.v
More file actions
196 lines (179 loc) · 5.25 KB
/
TOP.v
File metadata and controls
196 lines (179 loc) · 5.25 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//////////////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright © 2018 Alper Said Soylu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////////
//
// Project:
// SELF-SYNCHRONIZING STREAM CIPHER WITH LINEAR FEEDBACK SHIFT REGISTER
//
// Module : TOP
// Author : Alper Said Soylu
// Date : 12/27/2018
//
//////////////////////////////////////////////////////////////////////////////////
`timescale 1ns / 1ps
module TOP(
input clk, res, start, conf,
input [7:0] data_in,
input [7:0] address_in,
output reg done, busy,
output valid,
output [7:0] data_out
);
//submodule input registers
reg pico_res;
//submodule IO wires
wire ram_wren, ram_res;
wire [7:0] ram_dataout, ram_datain, ram_address;
wire [7:0] lfsr_out, lfsr_in;
wire lfsr_valid,lfsr_res;
wire [9:0] instr_address ;
wire [17:0] instruction ;
wire [7:0] port_id ;
wire write_strobe, read_strobe, interrupt_ack, interrupt ;
wire [7:0] in_port, out_port ;
//submodule instantiations
LFSR lfsr_inst (
.clk(clk),
.res(lfsr_res),
.valid(lfsr_valid),
.seed(lfsr_in),
.keystream(lfsr_out)
);
RAM #(.DATA_WIDTH(8),.ADDRESS_WIDTH(8)) ram_inst (
.clk(clk),
.res(ram_res),
.wren(ram_wren),
.address(ram_address),
.datain(ram_datain),
.dataout(ram_dataout)
);
kcpsm3 pico_inst (
.address(instr_address),
.instruction(instruction),
.port_id(port_id),
.write_strobe(write_strobe),
.out_port(out_port),
.read_strobe(read_strobe),
.in_port(in_port),
.interrupt(interrupt),
.interrupt_ack(interrupt_ack),
.reset(pico_res),
.clk(clk)
);
// Instantiate the module
SSSC instr_memory (
.address(instr_address),
.instruction(instruction),
.clk(clk)
);
//state labels
parameter DATA_WRITE = 3'b000;
parameter IDLE = 3'b001;
parameter START = 3'b011;
parameter PUBKEY_READ = 3'b010;
//
parameter PUBKEY_STORE = 3'b110;
parameter DATA_READ = 3'b111;
parameter KEYSTREAM_READ = 3'b101;
parameter CIPHER_OUT = 3'b100;
//state register
reg [7:0] state;
wire last;
//state logic
always @( posedge clk or posedge res ) begin
if ( res ) begin
state <= IDLE;
end else begin
case(state)
DATA_WRITE : if ( ~conf ) state <= IDLE;
IDLE : begin
if ( pico_res ) begin // ensuring to reset picoblaze two clock cycles
state <= START;
end else if ( conf & ~start ) begin
state <= DATA_WRITE;
end
end
START : if ( read_strobe ) state <= PUBKEY_READ;
PUBKEY_READ : if ( write_strobe ) state <= CIPHER_OUT;
PUBKEY_STORE : if ( read_strobe ) state <= DATA_READ; //removed
DATA_READ : if ( read_strobe ) state <= KEYSTREAM_READ;
KEYSTREAM_READ : if ( write_strobe ) state <= CIPHER_OUT;
CIPHER_OUT : begin
if ( last ) begin
state <= IDLE;//BUG
end else if ( read_strobe ) begin
state <= DATA_READ;
end
end
endcase
end
end
//continious assignments
assign interrupt = 0;
assign ram_datain = data_in;
assign ram_res = 0;
assign last = ( port_id == 8'hFF );
assign ram_wren = conf & ~busy;
assign ram_address = ram_wren ? address_in : port_id;
assign lfsr_in = out_port;
assign lfsr_valid = write_strobe;
assign lfsr_res = ( state == PUBKEY_READ ) & lfsr_valid;
assign data_out = out_port;
assign valid = ( state == KEYSTREAM_READ ) & write_strobe;
parameter RAM = 0, LFSR = 1;
reg select_input;
assign in_port = ( select_input == RAM ) ? ram_dataout : lfsr_out;
//IO logic
always @( posedge clk or posedge res ) begin
if ( res ) begin
pico_res <= 0;
busy <= 1;
done <= 0;
end else begin
case(state)
IDLE : begin
busy <= 0;
done <= 0;
pico_res <= start;
if ( pico_res ) begin
pico_res <= 0;
busy <= 1;
select_input <= RAM;
end
end
START : begin
if ( read_strobe ) begin
pico_res <= 0;
end
end
DATA_READ : begin
select_input <= LFSR;
if ( read_strobe ) begin
done <= last;
end
end
KEYSTREAM_READ : begin select_input <= RAM; end
default: begin end
endcase
end
end
endmodule