Skip to content

Commit 083eb70

Browse files
committed
add readme and source code
1 parent a2012c2 commit 083eb70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2219
-0
lines changed

accum.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
module accum( in, out, ena, clk, rst); // a register, to storage result after computing
4+
input clk,rst,ena;
5+
input [7:0] in;
6+
output reg [7:0] out;
7+
8+
always @(posedge clk or negedge rst) begin
9+
if(!rst) out <= 8'd0;
10+
else begin
11+
if(ena) out <= in;
12+
else out <= out;
13+
end
14+
end
15+
16+
endmodule

addr_mux.v

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
module addr_mux(addr, sel, ir_ad, pc_ad);
4+
// Address multiplexer
5+
// to choose address of instruction register or address of program counter
6+
input [7:0] ir_ad, pc_ad;
7+
input sel;
8+
output [7:0] addr;
9+
10+
assign addr = (sel)? ir_ad:pc_ad;
11+
12+
endmodule

alu.v

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
`timescale 1ns / 1ps
2+
//////////////////////////////////////////////////////////////////////////////////
3+
module alu(alu_out, alu_in, accum, op);// arithmetic logic unit
4+
// to perform arithmetic and logic operations.
5+
input [2:0] op;
6+
input [7:0] alu_in,accum;
7+
output reg [7:0] alu_out;
8+
9+
parameter NOP=3'b000,
10+
LDO=3'b001,
11+
LDA=3'b010,
12+
STO=3'b011,
13+
PRE=3'b100,
14+
ADD=3'b101,
15+
LDM=3'b110,
16+
HLT=3'b111;
17+
18+
19+
always @(*) begin
20+
casez(op)
21+
NOP: alu_out = accum;
22+
HLT: alu_out = accum;
23+
LDO: alu_out = alu_in;
24+
LDA: alu_out = alu_in;
25+
STO: alu_out = accum;
26+
PRE: alu_out = alu_in;
27+
ADD: alu_out = accum+alu_in;
28+
LDM: alu_out = accum;
29+
default: alu_out = 8'bzzzz_zzzz;
30+
endcase
31+
end
32+
33+
34+
endmodule

assets/CPU-comp-right.png

49.1 KB

assets/CPU-comp-tigh.png

19.1 KB

assets/CPU-comp.png

53.2 KB

assets/RTL Viewer.png

46.9 KB

assets/RTL_Viewer_s.png

45.9 KB

assets/accum.png

3.38 KB

assets/alu.png

17.5 KB

assets/controller-tb-result-1-0.png

92.8 KB

assets/fsm.pdf

30.9 KB
Binary file not shown.

assets/fsm.png

35.1 KB

assets/ir.png

16 KB

assets/ldo-debug.png

191 KB

assets/mux.png

3.6 KB

assets/mv.bmp

2.81 MB
Binary file not shown.

assets/mv2.png

178 KB

assets/mv3.png

206 KB

assets/pc.png

5.99 KB

assets/ram.png

29.2 KB

assets/reg.png

21.5 KB

assets/reg1.png

191 KB

assets/rom-tb-result-1-1.png

66.4 KB

assets/rom-tb-result-1-2.png

72.2 KB

assets/rom-tb-result-2.png

164 KB

assets/rom-tb-result.png

196 KB

assets/rom.png

20.7 KB

assets/s0.png

18.4 KB

assets/s1.png

26.4 KB

assets/schematic.pdf

55.9 KB
Binary file not shown.

assets/schematic.png

84.6 KB

assets/short-ins-caption.png

8.75 KB

assets/short-ins.png

5.41 KB

assets/short-long-caption.png

14.8 KB

assets/short-long.png

7.77 KB

assets/sidle.png

173 KB

assets/sm.pdf

26.4 KB
Binary file not shown.

assets/von.png

22.9 KB

assets/von0.pdf

46.4 KB
Binary file not shown.

assets/von0.tif

6.3 MB
Binary file not shown.

assets/wave.png

277 KB

assets/wv1.png

184 KB

controller.v

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
module controller_purify(ins, clk, rst, write_r, read_r, PC_en, fetch, ac_ena, ram_ena, rom_ena,ram_write, ram_read, rom_read, ad_sel);
2+
3+
input clk, rst; // clock, reset
4+
input [2:0] ins; // instructions, 3 bits, 8 types
5+
6+
// Enable signals
7+
output reg write_r, read_r, PC_en, ac_ena, ram_ena, rom_ena;
8+
9+
// ROM: where instructions are storaged. Read only.
10+
// RAM: where data is storaged, readable and writable.
11+
output reg ram_write, ram_read, rom_read, ad_sel;
12+
13+
output reg [1:0] fetch; // 01: to fetch from RAM/ROM; 10: to fetch from REG
14+
15+
// State code(current state)
16+
reg [3:0] state; // current state
17+
reg [3:0] next_state; // next state
18+
19+
20+
// instruction code
21+
parameter NOP=3'b000, // no operation
22+
LDO=3'b001, // load ROM to register
23+
LDA=3'b010, // load RAM to register
24+
STO=3'b011, // Store intermediate results to accumulator
25+
PRE=3'b100, // Prefetch Data from Address
26+
ADD=3'b101, // Adds the contents of the memory address or integer to the accumulator
27+
LDM=3'b110, // Load Multiple
28+
HLT=3'b111; // Halt
29+
30+
// state code
31+
parameter Sidle=4'hf,
32+
S0=4'd0,
33+
S1=4'd1,
34+
S2=4'd2,
35+
S3=4'd3,
36+
S4=4'd4,
37+
S5=4'd5,
38+
S6=4'd6,
39+
S7=4'd7,
40+
S8=4'd8,
41+
S9=4'd9,
42+
S10=4'd10,
43+
S11=4'd11,
44+
S12=4'd12;
45+
46+
//PART A: D flip latch; State register
47+
always @(posedge clk or negedge rst)
48+
begin
49+
if(!rst) state<=Sidle;
50+
//current_state <= Sidle;
51+
else state<=next_state;
52+
//current_state <= next_state;
53+
end
54+
55+
//PART B: Next-state combinational logic
56+
always @*
57+
begin
58+
case(state)
59+
S1: begin
60+
if (ins==NOP) next_state=S0;
61+
else if (ins==HLT) next_state=S2;
62+
else if (ins==PRE | ins==ADD) next_state=S9;
63+
else if (ins==LDM) next_state=S11;
64+
else next_state=S3;
65+
end
66+
67+
S4: begin
68+
if (ins==LDA | ins==LDO) next_state=S5;
69+
//else if (ins==STO) next_state=S7;
70+
else next_state=S7; // ---Note: there are only 3 long instrucions. So, all the cases included. if (counter_A==2*b11)
71+
end
72+
Sidle: next_state=S0;
73+
S0: next_state=S1;
74+
S2: next_state=S2;
75+
S3: next_state=S4;
76+
S5: next_state=S6;
77+
S6: next_state=S0;
78+
S7: next_state=S8;
79+
S8: next_state=S0;
80+
S9: next_state=S10;
81+
S10: next_state=S0;
82+
S11: next_state=S12;
83+
S12: next_state=S0;
84+
default: next_state=Sidle;
85+
endcase
86+
end
87+
88+
// another style
89+
//PART C: Output combinational logic
90+
always@*
91+
begin
92+
case(state)
93+
// --Note: for each statement, we concentrate on the current state, not next_state
94+
// because it is combinational logic.
95+
Sidle: begin
96+
write_r=1'b0;
97+
read_r=1'b0;
98+
PC_en=1'b0;
99+
ac_ena=1'b0;
100+
ram_ena=1'b0;
101+
rom_ena=1'b0;
102+
ram_write=1'b0;
103+
ram_read=1'b0;
104+
rom_read=1'b0;
105+
ad_sel=1'b0;
106+
fetch=2'b00;
107+
end
108+
S0: begin // load IR
109+
write_r=0;
110+
read_r=0;
111+
PC_en=0;
112+
ac_ena=0;
113+
ram_ena=0;
114+
rom_ena=1;
115+
ram_write=0;
116+
ram_read=0;
117+
rom_read=1;
118+
ad_sel=0;
119+
fetch=2'b01;
120+
end
121+
S1: begin
122+
write_r=0;
123+
read_r=0;
124+
PC_en=1;
125+
ac_ena=0;
126+
ram_ena=0;
127+
ram_write=0;
128+
ram_read=0;
129+
rom_ena=1;
130+
rom_read=1;
131+
ad_sel=0;
132+
fetch=2'b00;
133+
end
134+
S2: begin
135+
write_r=0;
136+
read_r=0;
137+
PC_en=0;
138+
ac_ena=0;
139+
ram_ena=0;
140+
rom_ena=0;
141+
ram_write=0;
142+
ram_read=0;
143+
rom_read=0;
144+
ad_sel=0;
145+
fetch=2'b00;
146+
end
147+
S3: begin
148+
write_r=0;
149+
read_r=0;
150+
PC_en=0;
151+
ac_ena=1;
152+
ram_ena=0;
153+
rom_ena=1;
154+
ram_write=0;
155+
ram_read=0;
156+
rom_read=1;
157+
ad_sel=0;
158+
fetch=2'b10;
159+
end
160+
S4: begin
161+
write_r=0;
162+
read_r=0;
163+
PC_en=1;
164+
ac_ena=1;
165+
ram_ena=0;
166+
ram_write=0;
167+
ram_read=0;
168+
rom_ena=1;
169+
rom_read=1;
170+
ad_sel=0;
171+
fetch=2'b10;
172+
end
173+
S5: begin
174+
if (ins==LDO)
175+
begin
176+
write_r=1;
177+
read_r=0;
178+
PC_en=0;
179+
ac_ena=1;
180+
ram_ena=0;
181+
ram_write=0;
182+
ram_read=0;
183+
rom_ena=1;
184+
rom_read=1;
185+
ad_sel=1;
186+
fetch=2'b01;
187+
end
188+
else
189+
begin
190+
write_r=1;
191+
read_r=0;
192+
PC_en=0;
193+
ac_ena=1;
194+
ram_ena=1;
195+
ram_write=0;
196+
ram_read=1;
197+
rom_ena=0;
198+
rom_read=0;
199+
ad_sel=1;
200+
fetch=2'b01;
201+
end
202+
end
203+
S6: begin
204+
205+
write_r=1'b0;
206+
read_r=1'b0;
207+
PC_en=1'b0; //** not so sure, log: change 1 to 0
208+
ac_ena=1'b0;
209+
ram_ena=1'b0;
210+
rom_ena=1'b0;
211+
ram_write=1'b0;
212+
ram_read=1'b0;
213+
rom_read=1'b0;
214+
ad_sel=1'b0;
215+
fetch=2'b00;
216+
end
217+
218+
S7: begin // STO, reg->ram. step1. read REG
219+
write_r=0;
220+
read_r=1;
221+
PC_en=0;
222+
ac_ena=0;
223+
ram_ena=0;
224+
rom_ena=0;
225+
ram_write=0;
226+
ram_read=0;
227+
rom_read=0;
228+
ad_sel=0;
229+
fetch=2'b00;
230+
end
231+
S8: begin // STO, step2, write RAM
232+
write_r=0;
233+
read_r=1;
234+
PC_en=0;
235+
ac_ena=0;
236+
rom_read=0;
237+
rom_ena=0;
238+
239+
ram_ena=1;
240+
ram_write=1;
241+
ram_read=0;
242+
243+
ad_sel=1;
244+
fetch=2'b00; //fetch=2'b10, ram_ena=1, ram_write=1, ad_sel=1;
245+
end
246+
S9: begin
247+
if (ins==PRE) // REG->ACCUM
248+
begin
249+
write_r=0;
250+
read_r=1;
251+
PC_en=0;
252+
ac_ena=1;
253+
ram_ena=0;
254+
rom_ena=0;
255+
ram_write=0;
256+
ram_read=0;
257+
rom_read=0;
258+
ad_sel=0;
259+
fetch=2'b00;
260+
end
261+
else
262+
begin
263+
write_r=0;
264+
read_r=1;
265+
PC_en=0;
266+
ac_ena=1;
267+
ram_ena=0;
268+
rom_ena=0;
269+
ram_write=0;
270+
ram_read=0;
271+
rom_read=0;
272+
ad_sel=0;
273+
274+
fetch=2'b00;
275+
end
276+
end
277+
S10: begin
278+
write_r=0;
279+
read_r=1;
280+
PC_en=0;
281+
ac_ena=0;
282+
ram_ena=0;
283+
rom_ena=0;
284+
ram_write=0;
285+
ram_read=0;
286+
rom_read=0;
287+
ad_sel=0;
288+
fetch=2'b00;
289+
end
290+
S11: begin // LDM, step1, write reg
291+
write_r=1;
292+
read_r=0;
293+
PC_en=0;
294+
ac_ena=1;
295+
ram_ena=0;
296+
297+
ram_write=0;
298+
ram_read=0;
299+
rom_ena=1;
300+
rom_read=1;
301+
ad_sel=0;
302+
fetch=2'b00;
303+
304+
end
305+
S12: begin
306+
write_r=0;
307+
read_r=0;
308+
PC_en=0;
309+
ac_ena=0;
310+
ram_ena=0;
311+
rom_ena=0;
312+
ram_write=0;
313+
ram_read=0;
314+
rom_read=0;
315+
ad_sel=0;
316+
fetch=2'b00;
317+
end
318+
default: begin
319+
write_r=0;
320+
read_r=0;
321+
PC_en=0;
322+
ac_ena=0;
323+
ram_ena=0;
324+
rom_ena=0;
325+
ram_write=0;
326+
ram_read=0;
327+
rom_read=0;
328+
ad_sel=0;
329+
fetch=2'b00;
330+
end
331+
endcase
332+
end
333+
endmodule

0 commit comments

Comments
 (0)