Compare commits

..
5 Commits
Author SHA1 Message Date
austin c43955f8e8 Update pin constraint file 2023-03-04 17:57:05 -08:00
austin 3c0263fc05 Add SAP-3 2023-03-03 10:45:13 -08:00
austin eb7cc9dc79 Add rst and remove en from SAP-1 and SAP-2 2023-02-16 19:25:49 -08:00
austin 03d0d5c982 Add control ROM spreadsheet 2023-01-16 11:30:20 -08:00
austin 88bc37398f Update README 2023-01-15 14:00:53 -08:00
32 changed files with 2075 additions and 457 deletions

No files matched your search

+7 -2
View File
@@ -1,8 +1,13 @@
# FPGA Computer
8-bit computer modeled after the SAP-1 from the book [Digital Computer
8-bit computer modeled after the Simple-As-Possible computers from the book [Digital Computer
Electronics](https://www.goodreads.com/book/show/942643.Digital_Computer_Electronics) by Malvino and
Brown.
[Here is a blog post about it.](https://austinmorlan.com/posts/fpga_computer_sap1/)
The SAP-3 is the final iteration of the computer and shares an instruction set with the Intel
8080/8085, minus a few instructions.
* [Blog post about the SAP-1.](https://austinmorlan.com/posts/fpga_computer_sap1/)
* [Blog post about the SAP-2.](https://austinmorlan.com/posts/fpga_computer_sap2/)
* [Blog post about the SAP-3.](https://austinmorlan.com/posts/fpga_computer_sap3/)
+34 -4
View File
@@ -35,6 +35,17 @@ SAP2_MODULES="
SAP2_TOP=$SRC_DIR/sap2/top.v
SAP2_TB=$SRC_DIR/sap2/top_tb.v
SAP3_MODULES="
$SRC_DIR/sap3/alu.v
$SRC_DIR/sap3/clock.v
$SRC_DIR/sap3/controller.v
$SRC_DIR/sap3/ir.v
$SRC_DIR/sap3/memory.v
$SRC_DIR/sap3/reg_file.v"
SAP3_TOP=$SRC_DIR/sap3/top.v
SAP3_TB=$SRC_DIR/sap3/top_tb.v
if [ -z "$COMMAND" ]; then
mkdir -p $BUILD_DIR/sap1
@@ -54,6 +65,15 @@ if [ -z "$COMMAND" ]; then
$BIN_DIR/icepack hardware.asc hardware.bin
popd > /dev/null
mkdir -p $BUILD_DIR/sap3
pushd $BUILD_DIR/sap3 > /dev/null
$BIN_DIR/yosys -q -p "synth_ice40 -json hardware.json" $SAP3_MODULES $SAP3_TOP
$BIN_DIR/nextpnr-ice40 --lp8k --package cm81 --json hardware.json --asc hardware.asc --pcf $SRC_DIR/pins.pcf -q
$BIN_DIR/icepack hardware.asc hardware.bin
popd > /dev/null
elif [ "$COMMAND" == "init" ]; then
virtualenv .env
source .env/bin/activate
@@ -70,13 +90,14 @@ elif [ "$COMMAND" == "program" ]; then
exit 1
elif [ "$VERSION" == "sap1" ]; then
pushd $BUILD_DIR/sap1 > /dev/null
sudo tinyprog -p hardware.bin
popd > /dev/null
elif [ "$VERSION" == "sap2" ]; then
pushd $BUILD_DIR/sap2 > /dev/null
elif [ "$VERSION" == "sap3" ]; then
pushd $BUILD_DIR/sap3 > /dev/null
fi
sudo tinyprog -p hardware.bin
popd > /dev/null
fi
elif [ "$COMMAND" == "sim" ]; then
if [ -z "$VERSION" ]; then
echo "sim needs a version"
@@ -101,8 +122,17 @@ elif [ "$COMMAND" == "sim" ]; then
popd > /dev/null
gtkwave $BUILD_DIR/sap2/top_tb.vcd $BUILD_DIR/sap2/top_tb.gtkw
fi
elif [ "$VERSION" == "sap3" ]; then
mkdir -p $BUILD_DIR/sap3
pushd $SRC_DIR/sap3 > /dev/null
$BIN_DIR/iverilog -o $BUILD_DIR/sap3/top_tb $SAP3_MODULES $SAP3_TB
$BIN_DIR/vvp $BUILD_DIR/sap3/top_tb
mv top_tb.vcd $BUILD_DIR/sap3
popd > /dev/null
gtkwave $BUILD_DIR/sap3/top_tb.vcd $BUILD_DIR/sap3/top_tb.gtkw
fi
else
echo "Invalid arguments"
fi
Binary file not shown.
+9
View File
@@ -1,2 +1,11 @@
set_io --warn-no-port CLK B2 # input
set_io --warn-no-port PIN_24 A6 # input
set_io --warn-no-port PIN_4 C2 # output
set_io --warn-no-port PIN_5 C1 # output
set_io --warn-no-port PIN_6 D2 # output
set_io --warn-no-port PIN_7 D1 # output
set_io --warn-no-port PIN_8 E2 # output
set_io --warn-no-port PIN_9 E1 # output
set_io --warn-no-port PIN_10 G2 # output
set_io --warn-no-port PIN_11 H1 # output
+3 -6
View File
@@ -2,13 +2,10 @@ module adder(
input[7:0] a,
input[7:0] b,
input sub,
input en,
output[7:0] bus);
output[7:0] out
);
assign bus =
(en) ?
((sub) ? a-b : a+b) :
8'bz;
assign out = (sub) ? a-b : a+b;
endmodule
+2 -1
View File
@@ -1,7 +1,8 @@
module clock(
input hlt,
input clk_in,
output clk_out);
output clk_out
);
assign clk_out = (hlt) ? 1'b0 : clk_in;
+38 -51
View File
@@ -1,7 +1,9 @@
module controller(
input clk,
input[3:0] instr,
output reg[11:0] ctrl_word);
input rst,
input[3:0] opcode,
output[11:0] out
);
localparam SIG_HLT = 11;
localparam SIG_PC_INC = 10;
@@ -21,103 +23,88 @@ localparam OP_ADD = 4'b0001;
localparam OP_SUB = 4'b0010;
localparam OP_HLT = 4'b1111;
reg[2:0] stage = 0;
reg[2:0] stage;
reg[11:0] ctrl_word;
always @(negedge clk) begin
if (stage == 6) begin
stage <= 1;
always @(negedge clk, posedge rst) begin
if (rst) begin
stage <= 0;
end else begin
if (stage == 5) begin
stage <= 0;
end else begin
stage <= stage + 1;
end
end
end
always @(*) begin
case (stage)
1: begin
ctrl_word = 12'b0;
case (stage)
0: begin
ctrl_word[SIG_PC_EN] = 1;
ctrl_word[SIG_MEM_LOAD] = 1;
end
2: begin
ctrl_word = 12'b0;
1: begin
ctrl_word[SIG_PC_INC] = 1;
end
3: begin
ctrl_word = 12'b0;
2: begin
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_IR_LOAD] = 1;
end
4: begin
case (instr)
3: begin
case (opcode)
OP_LDA: begin
ctrl_word = 12'b0;
ctrl_word[SIG_IR_EN] = 1;
ctrl_word[SIG_MEM_LOAD] = 1;
end
OP_ADD: begin
ctrl_word = 12'b0;
ctrl_word[SIG_IR_EN] = 1;
ctrl_word[SIG_MEM_LOAD] = 1;
end
OP_SUB: begin
ctrl_word = 12'b0;
ctrl_word[SIG_IR_EN] = 1;
ctrl_word[SIG_MEM_LOAD] = 1;
end
OP_HLT: begin
ctrl_word = 12'b0;
ctrl_word[SIG_HLT] = 1;
end
default: begin
ctrl_word = 12'b0;
endcase
end
4: begin
case (opcode)
OP_LDA: begin
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_A_LOAD] = 1;
end
OP_ADD: begin
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_B_LOAD] = 1;
end
OP_SUB: begin
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_B_LOAD] = 1;
end
endcase
end
5: begin
case (instr)
OP_LDA: begin
ctrl_word = 12'b0;
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_A_LOAD] = 1;
end
case (opcode)
OP_ADD: begin
ctrl_word = 12'b0;
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_B_LOAD] = 1;
end
OP_SUB: begin
ctrl_word = 12'b0;
ctrl_word[SIG_MEM_EN] = 1;
ctrl_word[SIG_B_LOAD] = 1;
end
default: begin
ctrl_word = 12'b0;
end
endcase
end
6: begin
case (instr)
OP_ADD: begin
ctrl_word = 12'b0;
ctrl_word[SIG_ADDER_EN] = 1;
ctrl_word[SIG_A_LOAD] = 1;
end
OP_SUB: begin
ctrl_word = 12'b0;
ctrl_word[SIG_ADDER_SUB] = 1;
ctrl_word[SIG_ADDER_EN] = 1;
ctrl_word[SIG_A_LOAD] = 1;
end
default: begin
ctrl_word = 12'b0;
end
endcase
end
default: begin
ctrl_word = 12'b0;
end
endcase
end
assign out = ctrl_word;
endmodule
+8 -9
View File
@@ -1,23 +1,22 @@
module ir(
input clk,
input clr,
input rst,
input load,
input en,
inout[7:0] bus,
output[3:0] instr);
input[7:0] bus,
output[7:0] out
);
reg[7:0] ir = 0;
reg[7:0] ir;
always @(posedge clk or posedge clr) begin
if (clr) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
ir <= 8'b0;
end else if (load) begin
ir <= bus;
end
end
assign instr = ir[7:4];
assign bus = (en) ? ir[3:0] : 8'bz;
assign out = ir;
endmodule
+9 -6
View File
@@ -1,24 +1,27 @@
module memory(
input clk,
input rst,
input load,
input en,
inout[7:0] bus
input[7:0] bus,
output[7:0] out
);
initial begin
$readmemh("program.bin", ram);
end
reg[3:0] mar = 0;
reg[3:0] mar;
reg[7:0] ram[0:15];
always @(posedge clk) begin
if (load) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
mar <= 4'b0;
end else if (load) begin
mar <= bus[3:0];
end
end
assign bus = (en) ? ram[mar] : 8'bz;
assign out = ram[mar];
endmodule
+6 -7
View File
@@ -1,22 +1,21 @@
module pc(
input clk,
input clr,
input rst,
input inc,
input en,
output[7:0] bus
output[7:0] out
);
reg[3:0] pc = 0;
reg[3:0] pc;
always @(posedge clk or posedge clr) begin
if (clr) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
pc <= 4'b0;
end else if (inc) begin
pc <= pc + 1;
end
end
assign bus = (en) ? pc : 8'bz;
assign out = pc;
endmodule
+10 -8
View File
@@ -1,20 +1,22 @@
module reg_a(
input clk,
input rst,
input load,
input en,
inout[7:0] bus,
output[7:0] val);
input[7:0] bus,
output[7:0] out
);
reg[7:0] reg_a = 0;
reg[7:0] reg_a;
always @(posedge clk) begin
if (load) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
reg_a <= 8'b0;
end else if (load) begin
reg_a <= bus;
end
end
assign bus = (en) ? reg_a : 8'bz;
assign val = reg_a;
assign out = reg_a;
endmodule
+9 -5
View File
@@ -1,18 +1,22 @@
module reg_b(
input clk,
input rst,
input load,
input[7:0] bus,
output[7:0] val);
output[7:0] out
);
reg[7:0] reg_b = 0;
reg[7:0] reg_b;
always @(posedge clk) begin
if (load) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
reg_b <= 8'b0;
end else if (load) begin
reg_b <= bus;
end
end
assign val = reg_b;
assign out = reg_b;
endmodule
+71 -45
View File
@@ -1,82 +1,108 @@
module top(
input CLK);
input CLK
);
wire[7:0] bus;
wire clk;
reg[7:0] bus;
always @(*) begin
if (ir_en) begin
bus = ir_out;
end else if (adder_en) begin
bus = adder_out;
end else if (a_en) begin
bus = a_out;
end else if (mem_en) begin
bus = mem_out;
end else if (pc_en) begin
bus = pc_out;
end else begin
bus = 8'b0;
end
end
wire rst;
wire hlt;
wire clk;
clock clock(
.hlt(hlt),
.clk_in(CLK),
.clk_out(clk)
);
wire clr;
wire pc_en;
wire pc_inc;
wire pc_en;
wire[7:0] pc_out;
pc pc(
.clk(clk),
.clr(clr),
.rst(rst),
.inc(pc_inc),
.en(pc_en),
.bus(bus)
.out(pc_out)
);
wire a_load;
wire a_en;
wire[7:0] a_val;
reg_a reg_a(
.clk(clk),
.load(a_load),
.en(a_en),
.bus(bus),
.val(a_val)
);
wire b_load;
wire[7:0] b_val;
reg_b reg_b(
.clk(clk),
.load(b_load),
.bus(bus),
.val(b_val)
);
wire adder_sub;
wire adder_en;
adder adder(
.a(a_val),
.b(b_val),
.sub(adder_sub),
.en(adder_en),
.bus(bus)
);
wire mar_load;
wire mem_en;
wire[7:0] mem_out;
memory mem(
.clk(clk),
.rst(rst),
.load(mar_load),
.en(mem_en),
.bus(bus)
.bus(bus),
.out(mem_out)
);
wire a_load;
wire a_en;
wire[7:0] a_out;
reg_a reg_a(
.clk(clk),
.rst(rst),
.load(a_load),
.bus(bus),
.out(a_out)
);
wire b_load;
wire[7:0] b_out;
reg_b reg_b(
.clk(clk),
.rst(rst),
.load(b_load),
.bus(bus),
.out(b_out)
);
wire adder_sub;
wire adder_en;
wire[7:0] adder_out;
adder adder(
.a(a_out),
.b(b_out),
.sub(adder_sub),
.out(adder_out)
);
wire ir_load;
wire ir_en;
wire[3:0] ir_instr;
wire[7:0] ir_out;
ir ir(
.clk(clk),
.clr(clr),
.rst(rst),
.load(ir_load),
.en(ir_en),
.bus(bus),
.instr(ir_instr)
.out(ir_out)
);
controller controller(
.clk(clk),
.instr(ir_instr),
.ctrl_word(
.rst(rst),
.opcode(ir_out[7:4]),
.out(
{
hlt,
pc_inc,
+71 -44
View File
@@ -1,8 +1,29 @@
module top_tb();
initial begin
$dumpfile("top_tb.vcd");
$dumpvars(0, top_tb);
rst = 1;
#1 rst = 0;
end
reg[7:0] bus;
always @(*) begin
if (ir_en) begin
bus = ir_out;
end else if (adder_en) begin
bus = adder_out;
end else if (a_en) begin
bus = a_out;
end else if (mem_en) begin
bus = mem_out;
end else if (pc_en) begin
bus = pc_out;
end else begin
bus = 8'b0;
end
end
reg clk_in = 0;
@@ -15,80 +36,86 @@ end
wire clk;
wire hlt;
wire clr;
wire[7:0] bus;
reg rst;
clock clock(
.hlt(hlt),
.clk_in(clk_in),
.clk_out(clk)
);
wire pc_en;
wire pc_inc;
wire[7:0] pc_out;
wire pc_en;
pc pc(
.clk(clk),
.clr(clr),
.rst(rst),
.inc(pc_inc),
.en(pc_en),
.bus(bus)
.out(pc_out)
);
wire a_load;
wire a_en;
wire[7:0] a_val;
reg_a reg_a(
.clk(clk),
.load(a_load),
.en(a_en),
.bus(bus),
.val(a_val)
);
wire b_load;
wire[7:0] b_val;
reg_b reg_b(
.clk(clk),
.load(b_load),
.bus(bus),
.val(b_val)
);
wire adder_sub;
wire adder_en;
adder adder(
.a(a_val),
.b(b_val),
.sub(adder_sub),
.en(adder_en),
.bus(bus)
);
wire mar_load;
wire mem_en;
wire[7:0] mem_out;
memory mem(
.clk(clk),
.rst(rst),
.load(mar_load),
.en(mem_en),
.bus(bus)
.bus(bus),
.out(mem_out)
);
wire a_load;
wire a_en;
wire[7:0] a_out;
reg_a reg_a(
.clk(clk),
.rst(rst),
.load(a_load),
.bus(bus),
.out(a_out)
);
wire b_load;
wire[7:0] b_out;
reg_b reg_b(
.clk(clk),
.rst(rst),
.load(b_load),
.bus(bus),
.out(b_out)
);
wire adder_sub;
wire adder_en;
wire[7:0] adder_out;
adder adder(
.a(a_out),
.b(b_out),
.sub(adder_sub),
.out(adder_out)
);
wire ir_load;
wire ir_en;
wire[3:0] ir_instr;
wire[7:0] ir_out;
ir ir(
.clk(clk),
.clr(clr),
.rst(rst),
.load(ir_load),
.en(ir_en),
.bus(bus),
.instr(ir_instr)
.out(ir_out)
);
controller controller(
.clk(clk),
.instr(ir_instr),
.ctrl_word(
.rst(rst),
.opcode(ir_out[7:4]),
.out(
{
hlt,
pc_inc,
+10 -7
View File
@@ -1,14 +1,15 @@
module alu(
input clk,
input rst,
input[7:0] a,
input[2:0] op,
input load,
input en,
inout[15:0] bus
input[15:0] bus,
output[7:0] out
);
reg[7:0] alu = 0;
reg[7:0] tmp = 0;
reg[7:0] alu;
reg[7:0] tmp;
localparam OP_ADD = 0;
localparam OP_SUB = 1;
@@ -19,8 +20,10 @@ localparam OP_CMA = 5;
localparam OP_RAL = 6;
localparam OP_RAR = 7;
always @(posedge clk) begin
if (load) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
tmp <= 8'b0;
end else if (load) begin
tmp <= bus[7:0];
end
end
@@ -58,7 +61,7 @@ always @(*) begin
endcase
end
assign bus = (en) ? alu : 16'bz;
assign out = alu;
endmodule
+27 -18
View File
@@ -1,8 +1,9 @@
module controller(
input clk,
input[7:0] instr,
input rst,
input[7:0] opcode,
input[1:0] flags,
output reg[33:0] ctrl_word
output[33:0] out
);
localparam SIG_END = 34;
@@ -48,33 +49,41 @@ localparam OP_JM = 8'hFA;
localparam FLAG_Z = 1;
localparam FLAG_S = 0;
reg[34:0] ctrl_word;
reg[34:0] ctrl_rom[0:4095];
initial begin
$readmemb("ctrl_rom.bin", ctrl_rom);
end
reg reset = 0;
reg[3:0] stage = 0;
always @(negedge clk) begin
if (reset) begin
reg[3:0] stage;
always @(negedge clk, posedge rst) begin
if (rst) begin
stage <= 0;
end else begin
if (stage_rst) begin
stage <= 0;
end else begin
stage <= stage + 1;
end
end
always @(*) begin
ctrl_word = ctrl_rom[{instr, stage}];
if ((instr == OP_JZ && stage == 4 && flags[FLAG_Z] == 0) ||
(instr == OP_JNZ && stage == 4 && flags[FLAG_Z] == 1) ||
(instr == OP_JM && stage == 4 && flags[FLAG_S] == 0))
begin
reset = 1;
end else begin
reset = ctrl_rom[{instr, stage}][SIG_END];
end
end
reg stage_rst;
always @(*) begin
ctrl_word = ctrl_rom[{opcode, stage}];
if ((opcode == OP_JZ && stage == 4 && flags[FLAG_Z] == 0) ||
(opcode == OP_JNZ && stage == 4 && flags[FLAG_Z] == 1) ||
(opcode == OP_JM && stage == 4 && flags[FLAG_S] == 0))
begin
stage_rst = 1;
end else begin
stage_rst = ctrl_rom[{opcode, stage}][SIG_END];
end
end
assign out = ctrl_word;
endmodule
+13 -10
View File
@@ -1,5 +1,6 @@
module flags(
input clk,
input rst,
input[7:0] a,
input[7:0] b,
input[7:0] c,
@@ -12,22 +13,24 @@ module flags(
localparam FLAG_Z = 1;
localparam FLAG_S = 0;
reg[1:0] data = 0;
reg[1:0] flags;
always @(negedge clk) begin
if (load_a) begin
data[FLAG_Z] <= (a == 0) ? 1'b1 : 1'b0;
data[FLAG_S] <= (a[7] == 1) ? 1'b1 : 1'b0;
always @(negedge clk, posedge rst) begin
if (rst) begin
flags <= 2'b0;
end else if (load_a) begin
flags[FLAG_Z] <= (a == 0) ? 1'b1 : 1'b0;
flags[FLAG_S] <= (a[7] == 1) ? 1'b1 : 1'b0;
end else if (load_b) begin
data[FLAG_Z] <= (b == 0) ? 1'b1 : 1'b0;
data[FLAG_S] <= (b[7] == 1) ? 1'b1 : 1'b0;
flags[FLAG_Z] <= (b == 0) ? 1'b1 : 1'b0;
flags[FLAG_S] <= (b[7] == 1) ? 1'b1 : 1'b0;
end else if (load_c) begin
data[FLAG_Z] <= (c == 0) ? 1'b1 : 1'b0;
data[FLAG_S] <= (c[7] == 1) ? 1'b1 : 1'b0;
flags[FLAG_Z] <= (c == 0) ? 1'b1 : 1'b0;
flags[FLAG_S] <= (c[7] == 1) ? 1'b1 : 1'b0;
end
end
assign out = data;
assign out = flags;
endmodule
+7 -7
View File
@@ -1,22 +1,22 @@
module ir(
input clk,
input clr,
input rst,
input load,
input[15:0] bus,
output[7:0] out
);
reg[7:0] data = 0;
reg[7:0] ir;
always @(posedge clk or posedge clr) begin
if (clr) begin
data <= 8'b0;
always @(posedge clk, posedge rst) begin
if (rst) begin
ir <= 8'b0;
end else if (load) begin
data <= bus[7:0];
ir <= bus[7:0];
end
end
assign out = data;
assign out = ir;
endmodule
+20 -22
View File
@@ -1,25 +1,31 @@
module memory(
input clk,
input rst,
input mar_loadh,
input mar_loadl,
input mdr_load,
input mdr_en,
input ram_load,
input ram_enh,
input ram_enl,
input call,
input ret,
inout[15:0] bus
input[15:0] bus,
output[15:0] out
);
initial begin
$readmemh("program.bin", ram);
end
reg[15:0] mar = 0;
reg[15:0] mdr = 0;
reg[15:0] mar;
reg[15:0] mdr;
reg[7:0] ram[0:65535];
always @(posedge rst) begin
mar <= 16'b0;
mdr <= 16'b0;
end
always @(posedge clk) begin
if (mar_loadh) begin
mar[15:8] <= bus[15:8];
@@ -33,30 +39,22 @@ always @(posedge clk) begin
mdr[7:0] <= bus[7:0];
end
if (ram_load) begin
ram[mar] <= mdr;
end
if (ram_enh) begin
mdr[15:8] <= ram[mar];
end
if (ram_enl) begin
mdr[7:0] <= ram[mar];
end
if (call) begin
ram[16'hFFFE] <= bus[15:8];
ram[16'hFFFF] <= bus[7:0];
end
if (ret) begin
mdr[15:8] <= ram[16'hFFFE];
mdr[7:0] <= ram[16'hFFFF];
end else if (call) begin
ram[16'hFFFE] <= bus[15:8];
ram[16'hFFFF] <= bus[7:0];
end else if (ram_enh) begin
mdr[15:8] <= ram[mar];
end else if (ram_enl) begin
mdr[7:0] <= ram[mar];
end else if (ram_load) begin
ram[mar] <= mdr;
end
end
assign bus = (mdr_en) ? mdr : 16'bz;
assign out = mdr;
endmodule
+7 -7
View File
@@ -1,16 +1,16 @@
module pc(
input clk,
input clr,
input rst,
input inc,
input load,
input en,
output[15:0] bus
input[15:0] bus,
output[15:0] out
);
reg[15:0] pc = 0;
reg[15:0] pc;
always @(posedge clk or posedge clr) begin
if (clr) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
pc <= 16'b0;
end else if (load) begin
pc <= bus;
@@ -19,7 +19,7 @@ always @(posedge clk or posedge clr) begin
end
end
assign bus = (en) ? pc : 16'bz;
assign out = pc;
endmodule
+7 -6
View File
@@ -1,17 +1,19 @@
module register(
input clk,
input rst,
input load,
input en,
input inc,
input dec,
inout[15:0] bus,
input[15:0] bus,
output[7:0] out
);
reg[7:0] data = 0;
reg[7:0] data;
always @(posedge clk) begin
if (load) begin
always @(posedge clk, posedge rst) begin
if (rst) begin
data <= 8'b0;
end else if (load) begin
data <= bus[7:0];
end else if (inc) begin
data <= data + 1;
@@ -20,7 +22,6 @@ always @(posedge clk) begin
end
end
assign bus = (en) ? {8'b0, data} : 16'bz;
assign out = data;
endmodule
+66 -96
View File
@@ -3,49 +3,57 @@ module top(
);
wire[15:0] bus;
wire clr;
reg[15:0] bus;
always @(*) begin
if (a_en) begin
bus = a_out;
end else if (b_en) begin
bus = b_out;
end else if (c_en) begin
bus = c_out;
end else if (alu_en) begin
bus = alu_out;
end else if (pc_en) begin
bus = pc_out;
end else if (mdr_en) begin
bus = mem_out;
end else begin
bus = 16'b0;
end
end
wire rst;
wire hlt;
wire clk;
clock clock(
// In
.hlt(hlt),
.clk_in(CLK),
// Out
.clk_out(clk)
);
wire pc_inc;
wire pc_load;
wire pc_en;
wire[15:0] pc_out;
pc pc(
// In
.clk(clk),
.clr(clr),
.rst(rst),
.inc(pc_inc),
.load(pc_load),
.en(pc_en),
// Output
.bus(bus)
.bus(bus),
.out(pc_out)
);
wire ir_load;
wire[7:0] ir_instr;
wire[7:0] ir_out;
ir ir(
// In
.clk(clk),
.clr(clr),
.rst(rst),
.load(ir_load),
// Inout
.bus(bus),
// Out
.out(ir_instr)
.out(ir_out)
);
wire mar_loadh;
@@ -57,153 +65,115 @@ wire ram_enh;
wire ram_enl;
wire call;
wire ret;
wire[15:0] mem_out;
memory mem(
// In
.clk(clk),
.rst(rst),
.mar_loadh(mar_loadh),
.mar_loadl(mar_loadl),
.mdr_load(mdr_load),
.mdr_en(mdr_en),
.ram_load(ram_load),
.ram_enh(ram_enh),
.ram_enl(ram_enl),
.call(call),
.ret(ret),
// Inout
.bus(bus)
.bus(bus),
.out(mem_out)
);
wire a_load;
wire a_en;
wire a_inc;
wire a_dec;
wire[7:0] a_val;
wire[7:0] a_out;
register reg_a(
// In
.clk(clk),
.rst(rst),
.load(a_load),
.en(a_en),
.inc(a_inc),
.dec(a_dec),
// Inout
.bus(bus),
// Out
.out(a_val)
.out(a_out)
);
wire b_load;
wire b_en;
wire b_inc;
wire b_dec;
wire[7:0] b_val;
wire[7:0] b_out;
register reg_b(
// In
.clk(clk),
.rst(rst),
.load(b_load),
.en(b_en),
.inc(b_inc),
.dec(b_dec),
// Inout
.bus(bus),
// Out
.out(b_val)
.out(b_out)
);
wire c_load;
wire c_en;
wire c_inc;
wire c_dec;
wire[7:0] c_val;
wire[7:0] c_out;
register reg_c(
// In
.clk(clk),
.rst(rst),
.load(c_load),
.en(c_en),
.inc(c_inc),
.dec(c_dec),
// Inout
.bus(bus),
// Out
.out(c_val)
.out(c_out)
);
wire[2:0] alu_op;
wire alu_load;
wire alu_en;
wire[7:0] alu_out;
alu alu(
// In
.clk(clk),
.a(a_val),
.rst(rst),
.a(a_out),
.load(alu_load),
.op(alu_op),
.en(alu_en),
// Out
.bus(bus)
.bus(bus),
.out(alu_out)
);
wire[1:0] flags_val;
wire[1:0] flags_out;
wire flags_lda;
wire flags_ldb;
wire flags_ldc;
flags flags(
// In
.clk(clk),
.a(a_val),
.b(b_val),
.c(c_val),
.rst(rst),
.a(a_out),
.b(b_out),
.c(c_out),
.load_a(flags_lda),
.load_b(flags_ldb),
.load_c(flags_ldc),
// Out
.out(flags_val)
.out(flags_out)
);
controller controller(
.clk(clk),
.instr(ir_instr),
.flags(flags_val),
.ctrl_word({
.rst(rst),
.opcode(ir_out),
.flags(flags_out),
.out({
hlt,
a_load,
a_en,
a_inc,
a_dec,
b_load,
b_en,
b_inc,
b_dec,
c_load,
c_en,
c_inc,
c_dec,
flags_lda,
flags_ldb,
flags_ldc,
alu_op,
alu_load,
alu_en,
a_load, a_en, a_inc, a_dec,
b_load, b_en, b_inc, b_dec,
c_load, c_en, c_inc, c_dec,
flags_lda, flags_ldb, flags_ldc,
alu_op, alu_load, alu_en,
ir_load,
pc_inc,
pc_load,
pc_en,
mar_loadh,
mar_loadl,
mdr_load,
pc_inc, pc_load, pc_en,
mar_loadh, mar_loadl, mdr_load,
mdr_en,
ram_load,
ram_enh,
ram_enl,
call,
ret})
ram_load, ram_enh, ram_enl,
call, ret})
);
endmodule
+66 -96
View File
@@ -4,8 +4,29 @@ module top_tb();
initial begin
$dumpfile("top_tb.vcd");
$dumpvars(0, top_tb);
rst = 1;
#1 rst = 0;
end
reg[15:0] bus;
always @(*) begin
if (a_en) begin
bus = a_out;
end else if (b_en) begin
bus = b_out;
end else if (c_en) begin
bus = c_out;
end else if (alu_en) begin
bus = alu_out;
end else if (pc_en) begin
bus = pc_out;
end else if (mdr_en) begin
bus = mem_out;
end else begin
bus = 16'b0;
end
end
reg clk_in = 0;
integer i;
@@ -15,49 +36,36 @@ initial begin
end
end
reg rst;
wire hlt;
wire clr = 0;
wire[15:0] bus;
wire clk;
clock clock(
// In
.hlt(hlt),
.clk_in(clk_in),
// Out
.clk_out(clk)
);
wire pc_inc;
wire pc_load;
wire pc_en;
wire[15:0] pc_out;
pc pc(
// In
.clk(clk),
.clr(clr),
.rst(rst),
.inc(pc_inc),
.load(pc_load),
.en(pc_en),
// Output
.bus(bus)
.bus(bus),
.out(pc_out)
);
wire ir_load;
wire[7:0] ir_val;
wire[7:0] ir_out;
ir ir(
// In
.clk(clk),
.clr(clr),
.rst(rst),
.load(ir_load),
// Inout
.bus(bus),
// Out
.out(ir_val)
.out(ir_out)
);
wire mar_loadh;
@@ -69,153 +77,115 @@ wire ram_enh;
wire ram_enl;
wire call;
wire ret;
wire[15:0] mem_out;
memory mem(
// In
.clk(clk),
.rst(rst),
.mar_loadh(mar_loadh),
.mar_loadl(mar_loadl),
.mdr_load(mdr_load),
.mdr_en(mdr_en),
.ram_load(ram_load),
.ram_enh(ram_enh),
.ram_enl(ram_enl),
.call(call),
.ret(ret),
// Inout
.bus(bus)
.bus(bus),
.out(mem_out)
);
wire a_load;
wire a_en;
wire a_inc;
wire a_dec;
wire[7:0] a_val;
wire[7:0] a_out;
register reg_a(
// In
.clk(clk),
.rst(rst),
.load(a_load),
.en(a_en),
.inc(a_inc),
.dec(a_dec),
// Inout
.bus(bus),
// Out
.out(a_val)
.out(a_out)
);
wire b_load;
wire b_en;
wire b_inc;
wire b_dec;
wire[7:0] b_val;
wire[7:0] b_out;
register reg_b(
// In
.clk(clk),
.rst(rst),
.load(b_load),
.en(b_en),
.inc(b_inc),
.dec(b_dec),
// Inout
.bus(bus),
// Out
.out(b_val)
.out(b_out)
);
wire c_load;
wire c_en;
wire c_inc;
wire c_dec;
wire[7:0] c_val;
wire[7:0] c_out;
register reg_c(
// In
.clk(clk),
.rst(rst),
.load(c_load),
.en(c_en),
.inc(c_inc),
.dec(c_dec),
// Inout
.bus(bus),
// Out
.out(c_val)
.out(c_out)
);
wire[2:0] alu_op;
wire alu_load;
wire alu_en;
wire[7:0] alu_out;
alu alu(
// In
.clk(clk),
.a(a_val),
.rst(rst),
.a(a_out),
.load(alu_load),
.op(alu_op),
.en(alu_en),
// Inout
.bus(bus)
.bus(bus),
.out(alu_out)
);
wire[1:0] flags_val;
wire[1:0] flags_out;
wire flags_lda;
wire flags_ldb;
wire flags_ldc;
flags flags(
// In
.clk(clk),
.a(a_val),
.b(b_val),
.c(c_val),
.rst(rst),
.a(a_out),
.b(b_out),
.c(c_out),
.load_a(flags_lda),
.load_b(flags_ldb),
.load_c(flags_ldc),
// Out
.out(flags_val)
.out(flags_out)
);
controller controller(
.clk(clk),
.instr(ir_val),
.flags(flags_val),
.ctrl_word({
.rst(rst),
.opcode(ir_out),
.flags(flags_out),
.out({
hlt,
a_load,
a_en,
a_inc,
a_dec,
b_load,
b_en,
b_inc,
b_dec,
c_load,
c_en,
c_inc,
c_dec,
flags_lda,
flags_ldb,
flags_ldc,
alu_op,
alu_load,
alu_en,
a_load, a_en, a_inc, a_dec,
b_load, b_en, b_inc, b_dec,
c_load, c_en, c_inc, c_dec,
flags_lda, flags_ldb, flags_ldc,
alu_op, alu_load, alu_en,
ir_load,
pc_inc,
pc_load,
pc_en,
mar_loadh,
mar_loadl,
mdr_load,
pc_inc, pc_load, pc_en,
mar_loadh, mar_loadl, mdr_load,
mdr_en,
ram_load,
ram_enh,
ram_enl,
call,
ret})
ram_load, ram_enh, ram_enl,
call, ret})
);
endmodule
+173
View File
@@ -0,0 +1,173 @@
module alu(
input clk,
input rst,
input cs,
input flags_we,
input a_we,
input a_store,
input a_restore,
input tmp_we,
input[4:0] op,
input[7:0] bus,
output[7:0] flags,
output[7:0] out
);
reg carry;
wire flg_c;
wire flg_z;
wire flg_p;
wire flg_s;
reg[7:0] acc;
reg[7:0] flg;
reg[7:0] act; // Internal
reg[7:0] tmp; // Internal
localparam FLG_Z = 0;
localparam FLG_C = 1;
localparam FLG_P = 2;
localparam FLG_S = 3;
localparam OP_ADD = 5'b00000;
localparam OP_ADC = 5'b00001;
localparam OP_SUB = 5'b00010;
localparam OP_SBB = 5'b00011;
localparam OP_ANA = 5'b00100;
localparam OP_XRA = 5'b00101;
localparam OP_ORA = 5'b00110;
localparam OP_CMP = 5'b00111;
localparam OP_RLC = 5'b01000;
localparam OP_RRC = 5'b01001;
localparam OP_RAL = 5'b01010;
localparam OP_RAR = 5'b01011;
localparam OP_DAA = 5'b01100; // Unsupported
localparam OP_CMA = 5'b01101;
localparam OP_STC = 5'b01110;
localparam OP_CMC = 5'b01111;
localparam OP_INR = 5'b10000;
localparam OP_DCR = 5'b10001;
assign flg_c = (carry == 1'b1);
assign flg_z = (acc[7:0] == 8'b0);
assign flg_s = acc[7];
assign flg_p = ~^acc[7:0];
always @(posedge clk, posedge rst) begin
if (rst) begin
acc <= 8'b0;
act <= 8'b0;
tmp <= 8'b0;
carry <= 1'b0;
end else begin
if (a_we) begin
acc <= bus;
end else if (a_restore) begin
acc <= act;
end else if (cs) begin
case (op)
OP_ADD: begin
{carry, acc} <= acc + tmp;
end
OP_ADC: begin
{carry, acc} <= acc + tmp + flg[FLG_C];
end
OP_SUB: begin
{carry, acc} <= acc - tmp;
end
OP_SBB: begin
{carry, acc} <= acc - tmp - flg[FLG_C];
end
OP_ANA: begin
{carry, acc} <= acc & tmp;
end
OP_XRA: begin
{carry, acc} <= acc ^ tmp;
end
OP_ORA: begin
{carry, acc} <= acc | tmp;
end
OP_CMP: begin
act <= acc - tmp;
end
OP_RLC: begin
carry <= acc[7];
acc <= acc << 1;
end
OP_RRC: begin
carry <= acc[0];
acc <= acc >> 1;
end
OP_RAL: begin
carry <= acc[7];
acc <= (acc << 1 | {7'b0, flg[FLG_C]});
end
OP_RAR: begin
carry <= acc[0];
acc <= (acc >> 1 | {flg[FLG_C], 7'b0});
end
OP_CMA: begin
acc <= ~acc;
end
OP_STC: begin
carry <= 1'b1;
end
OP_CMC: begin
carry <= ~flg[FLG_C];
end
OP_INR: begin
acc <= acc + 1;
end
OP_DCR: begin
acc <= acc - 1;
end
endcase
end
if (a_store)
act <= acc;
if (tmp_we)
tmp <= bus;
end
end
always @(negedge clk, posedge rst) begin
if (rst) begin
flg <= 8'b0;
end else if (flags_we) begin
flg <= bus;
end else begin
if (cs) begin
case (op)
OP_ADD, OP_ADC, OP_SUB, OP_SBB, OP_ANA, OP_XRA, OP_ORA: begin
flg[FLG_C] <= flg_c;
flg[FLG_Z] <= flg_z;
flg[FLG_S] <= flg_s;
flg[FLG_P] <= flg_p;
end
OP_CMP: begin
flg[FLG_Z] <= (act == 8'b0);
end
OP_INR, OP_DCR: begin
flg[FLG_Z] <= flg_z;
flg[FLG_S] <= flg_s;
flg[FLG_P] <= flg_p;
end
OP_RLC, OP_RRC, OP_RAL, OP_RAR, OP_STC, OP_CMC: begin
flg[FLG_C] <= flg_c;
end
endcase
end
end
end
assign flags = flg;
assign out = acc;
endmodule
+9
View File
@@ -0,0 +1,9 @@
module clock(
input hlt,
input clk_in,
output clk_out);
assign clk_out = (hlt) ? 1'b0 : clk_in;
endmodule
+928
View File
@@ -0,0 +1,928 @@
module controller(
input clk,
input rst,
input[7:0] opcode,
input[7:0] flags,
output[32:0] out
);
localparam DISPLAY = 32;
localparam HLT = 31;
localparam ALU_CS = 30;
localparam ALU_FLAGS_WE = 29;
localparam ALU_A_WE = 28;
localparam ALU_A_STORE = 27;
localparam ALU_A_RESTORE = 26;
localparam ALU_TMP_WE = 25;
localparam ALU_OP4 = 24;
localparam ALU_OP0 = 20;
localparam ALU_OE = 19;
localparam ALU_FLAGS_OE = 18;
localparam REG_RD_SEL4 = 17;
localparam REG_RD_SEL0 = 13;
localparam REG_WR_SEL4 = 12;
localparam REG_WR_SEL0 = 8;
localparam REG_EXT1 = 7;
localparam REG_EXT0 = 6;
localparam REG_OE = 5;
localparam REG_WE = 4;
localparam MEM_WE = 3;
localparam MEM_MAR_WE = 2;
localparam MEM_OE = 1;
localparam IR_WE = 0;
localparam REG_INC = 2'b01;
localparam REG_DEC = 2'b10;
localparam REG_INC2 = 2'b11;
localparam REG_BC = 5'b10000;
localparam REG_BC_B = 5'b00000;
localparam REG_BC_C = 5'b00001;
localparam REG_DE = 5'b10010;
localparam REG_DE_D = 5'b00010;
localparam REG_DE_E = 5'b00011;
localparam REG_HL = 5'b10100;
localparam REG_HL_H = 5'b00100;
localparam REG_HL_L = 5'b00101;
localparam REG_WZ = 5'b10110;
localparam REG_WZ_W = 5'b00110;
localparam REG_WZ_Z = 5'b00111;
localparam REG_PC = 5'b11000;
localparam REG_PC_P = 5'b01000;
localparam REG_PC_C = 5'b01001;
localparam REG_SP = 5'b11010;
localparam REG_SP_S = 5'b01010;
localparam REG_SP_P = 5'b01011;
reg[32:0] ctrl_word;
reg[3:0] stage;
reg stage_rst;
assign out = ctrl_word;
always @(negedge clk, posedge rst) begin
if (rst) begin
stage <= 0;
end else begin
if (stage_rst) begin
stage <= 0;
end else begin
stage <= stage + 1;
end
end
end
always @(*) begin
ctrl_word = 0;
stage_rst = 0;
if (stage == 0) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 1) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[IR_WE] = 1'b1;
end else if (stage == 2) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else begin
casez (opcode)
// NOP
8'o000: begin
stage_rst = 1'b1;
end
// HLT
8'o166: begin
if (stage == 3) begin
ctrl_word[HLT] = 1'b1;
end
end
// MOV Rd, M
// opcode[5:3] - Rd
8'o1?6: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
if (opcode[5:3] == 3'b111) begin
ctrl_word[ALU_A_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:3]};
ctrl_word[REG_WE] = 1'b1;
end
ctrl_word[MEM_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// MOV M, Rs
// opcode[2:0] - Rs
8'o16?: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
if (opcode[2:0] == 3'b111) begin
ctrl_word[ALU_OE] = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[2:0]};
ctrl_word[REG_OE] = 1'b1;
end
ctrl_word[MEM_WE] = 1'b1;
stage_rst = 1'b1;
end
end
// MOV Rd, Rs
// opcode[5:3] - Rd
// opcode[2:0] - Rs
8'o1??: begin
if (stage == 3) begin
if (opcode[2:0] == 3'b111) begin
ctrl_word[ALU_OE] = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[2:0]};
ctrl_word[REG_OE] = 1'b1;
end
if (opcode[5:3] == 3'b111) begin
ctrl_word[ALU_A_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:3]};
ctrl_word[REG_WE] = 1'b1;
end
stage_rst = 1'b1;
end
end
// INX, DCX
// opcode[5:4] - 16-bit Register
// opcode[3] - Dec(1) / Inc (0)
8'o0?3: begin
if (stage == 3) begin
if (opcode[5:4] == 2'b11)
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
else
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b10, opcode[5:4], 1'b0};
ctrl_word[REG_EXT1:REG_EXT0] = {opcode[3], ~opcode[3]};
stage_rst = 1'b1;
end
end
// INR/DCR M
// opcode[5:3] - Rs
// opcode[0] - INR (0), DCR (1)
8'o064: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[ALU_A_STORE] = 1'b1;
ctrl_word[ALU_A_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {4'b1000, opcode[0]};
end else if (stage == 6) begin
ctrl_word[ALU_OE] = 1'b1;
ctrl_word[ALU_A_RESTORE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
stage_rst = 1'b1;
end
end
// INR/DCR Rs
// opcode[5:3] - Rs
// opcode[0] - INR (0), DCR (1)
8'o0?4, 8'o0?5: begin
if (stage == 3) begin
if (opcode[5:3] == 3'b111) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = 5'b10000;
stage_rst = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:3]};
ctrl_word[REG_OE] = 1'b1;
ctrl_word[ALU_A_STORE] = 1'b1;
ctrl_word[ALU_A_WE] = 1'b1;
end
end else if (stage == 4) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {4'b1000, opcode[0]};
end else if (stage == 5) begin
ctrl_word[ALU_OE] = 1'b1;
ctrl_word[ALU_A_RESTORE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:3]};
ctrl_word[REG_WE] = 1'b1;
stage_rst = 1'b1;
end
end
// Arithmetic/Logic Set 0 (M)
// opcode[5:3] - ALU Op
8'o2?6: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[ALU_TMP_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {2'b0, opcode[5:3]};
stage_rst = 1'b1;
end
end
// Arithmetic/Logic Set 0
// opcode[2:0] - Rs
// opcode[5:3] - ALU Op
8'o2??: begin
if (stage == 3) begin
if (opcode[2:0] == 3'b111) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {2'b0, opcode[5:3]};
stage_rst = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[2:0]};
ctrl_word[REG_OE] = 1'b1;
end
ctrl_word[ALU_TMP_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {2'b0, opcode[5:3]};
stage_rst = 1'b1;
end
end
// Arithmetic/Logic Set 1
// opcode[5:3] - ALU Op
8'o0?7: begin
if (stage == 3) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {2'b01, opcode[5:3]};
stage_rst = 1'b1;
end
end
// Arithmetic/Logic Immediate
// opcode[5:3] - ALU Op
8'o3?6: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[ALU_TMP_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = {2'b0, opcode[5:3]};
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
stage_rst = 1'b1;
end
end
// MVI M, d8
8'o066: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, REG_WZ_W};
ctrl_word[REG_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, REG_WZ_W};
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
stage_rst = 1'b1;
end
end
// MVI Rd, d8
// opcode[5:3] - Rd
8'o0?6: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
if (opcode[5:3] == 3'b111) begin
ctrl_word[ALU_A_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:3]};
ctrl_word[REG_WE] = 1'b1;
end
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
stage_rst = 1'b1;
end
end
// LXI
// opcode[5:4] - Extended Register
8'o001, 8'o021, 8'o041, 8'o061: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
end else if (stage == 8) begin
if (opcode[5:4] == 2'b11)
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
else
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b10, opcode[5:4], 1'b0};
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
end else if (stage == 9) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
stage_rst = 1'b1;
end
end
// LDA/STA a16
// opcode[3]: STA (0) / LDA (1)
8'o062, 8'o072: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 10) begin
if (opcode[3] == 0) begin
ctrl_word[ALU_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else begin
ctrl_word[ALU_A_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end
stage_rst = 1'b1;
end
end
// STAX/LDAX Rs
// opcode[5:4] - Rs
// opcode[3] - STAX (0) / LDAX (1)
8'o002, 8'o012, 8'o022, 8'o032: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b0};
ctrl_word[REG_OE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b1};
ctrl_word[REG_OE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
if (opcode[3] == 1'b0) begin
ctrl_word[ALU_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else begin
ctrl_word[ALU_A_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end
stage_rst = 1'b1;
end
end
// SHLD, LHLD
// opcode[3] - SHLD (0) / LHLD (1)
8'o042, 8'o052: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[MEM_OE] = 1'b1;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 10) begin
if (opcode[3] == 1'b0) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL_H;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_HL_H;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end
end else if (stage == 11) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 12) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 13) begin
if (opcode[3] == 1'b0) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL_L;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_HL_L;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end
stage_rst = 1'b1;
end
end
// DAD
// opcode[5:4] - Extended Register
8'o011, 8'o031, 8'o051, 8'o071: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL_L;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[ALU_A_STORE] = 1'b1;
ctrl_word[ALU_A_WE] = 1'b1;
end else if (stage == 4) begin
if (opcode[5:4] == 2'b11) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP_P;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b1};
end
ctrl_word[REG_OE] = 1'b1;
ctrl_word[ALU_TMP_WE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = 5'b00000; // Add
end else if (stage == 6) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[ALU_OE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_HL_H;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[ALU_A_WE] = 1'b1;
end else if (stage == 8) begin
if (opcode[5:4] == 2'b11) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP_S;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b0};
end
ctrl_word[REG_OE] = 1'b1;
ctrl_word[ALU_TMP_WE] = 1'b1;
end else if (stage == 9) begin
ctrl_word[ALU_CS] = 1'b1;
ctrl_word[ALU_OP4:ALU_OP0] = 5'b00001; // Add w/ Carry
end else if (stage == 10) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[ALU_OE] = 1'b1;
ctrl_word[ALU_A_RESTORE] = 1'b1;
end else if (stage == 11) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_HL;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// JMP
8'o303: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// Jump Conditional
// opcode[5:4] - flag
// opcode[3] - set (1) / unset (0)
8'o3?2: begin
if (stage == 3) begin
if (flags[opcode[5:4]] != opcode[3]) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC2;
stage_rst = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// Call Conditional
// opcode[5:4] - flag
// opcode[3] - set (1) / unset (0)
8'o3?4: begin
if (stage == 3) begin
if (flags[opcode[5:4]] != opcode[3]) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC2;
stage_rst = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 10) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 11) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC_C;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 12) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 13) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 14) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC_P;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 15) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// CALL
8'o315: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 10) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 11) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC_C;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 12) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 13) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 14) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_PC_P;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 15) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// Return Conditional
// opcode[5:4] - flag
// opcode[3] - set (1) / unset (0)
8'o3?0: begin
if (stage == 3) begin
if (flags[opcode[5:4]] != opcode[3]) begin
stage_rst = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// RET
8'o311: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_W;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_WZ_Z;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 9) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_WZ;
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_WE] = 1'b1;
ctrl_word[REG_OE] = 1'b1;
stage_rst = 1'b1;
end
end
// PUSH Rs
// opcode[5:4] - Extended Register
8'o3?5: begin
if (stage == 3) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 4) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 5) begin
if (opcode[5:4] == 2'b11) begin // PSW
ctrl_word[ALU_OE] = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b0};
ctrl_word[REG_OE] = 1'b1;
end
ctrl_word[MEM_WE] = 1'b1;
end else if (stage == 6) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_DEC;
end else if (stage == 7) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 8) begin
if (opcode[5:4] == 2'b11) begin // PSW
ctrl_word[ALU_FLAGS_OE] = 1'b1;
end else begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = {2'b0, opcode[5:4], 1'b1};
ctrl_word[REG_OE] = 1'b1;
end
ctrl_word[MEM_WE] = 1'b1;
stage_rst = 1'b1;
end
end
// POP Rs
// opcode[5:4] - Extended Register
8'o301, 8'o321, 8'o341, 8'o361: begin
if (stage == 3) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 4) begin
if (opcode[5:4] == 2'b11) begin // PSW
ctrl_word[ALU_FLAGS_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:4], 1'b1};
ctrl_word[REG_WE] = 1'b1;
end
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 5) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
end else if (stage == 6) begin
ctrl_word[REG_RD_SEL4:REG_RD_SEL0] = REG_SP;
ctrl_word[REG_OE] = 1'b1;
ctrl_word[MEM_MAR_WE] = 1'b1;
end else if (stage == 7) begin
if (opcode[5:4] == 2'b11) begin // PSW
ctrl_word[ALU_A_WE] = 1'b1;
end else begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = {2'b0, opcode[5:4], 1'b0};
ctrl_word[REG_WE] = 1'b1;
end
ctrl_word[MEM_OE] = 1'b1;
end else if (stage == 8) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_SP;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
stage_rst = 1'b1;
end
end
// OUT
8'o323: begin
if (stage == 3) begin
ctrl_word[REG_WR_SEL4:REG_WR_SEL0] = REG_PC;
ctrl_word[REG_EXT1:REG_EXT0] = REG_INC;
ctrl_word[DISPLAY] = 1'b1;
stage_rst = 1'b1;
end
end
endcase
end
end
endmodule
+22
View File
@@ -0,0 +1,22 @@
module ir(
input clk,
input rst,
input we,
input[7:0] bus,
output[7:0] out
);
reg[7:0] ir;
always @(posedge clk, posedge rst) begin
if (rst) begin
ir <= 8'b0;
end else if (we) begin
ir <= bus;
end
end
assign out = ir;
endmodule
+32
View File
@@ -0,0 +1,32 @@
module memory(
input clk,
input rst,
input mar_we,
input ram_we,
input[15:0] bus,
output[7:0] out
);
initial begin
$readmemh("program.bin", ram);
end
reg[15:0] mar;
reg[7:0] ram[0:255];
always @(posedge clk, posedge rst) begin
if (rst)
mar <= 16'b0;
else if (mar_we)
mar <= bus;
end
always @(posedge clk) begin
if (ram_we)
ram[mar] <= bus[7:0];
end
assign out = ram[mar];
endmodule
+32
View File
@@ -0,0 +1,32 @@
31 f0 00 3e 01 06 00 d3
ff 4f 78 fe 01 79 ca 17
00 c2 20 00 c3 07 00 1f
fe 01 cc 29 00 c3 07 00
17 fe 80 cc 2c 00 c3 07
00 06 00 c9 06 01 c9 76
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
+72
View File
@@ -0,0 +1,72 @@
module reg_file(
input clk,
input rst,
input[4:0] rd_sel,
input[4:0] wr_sel,
input[1:0] ext,
input we,
input[15:0] data_in,
output[15:0] data_out
);
// 8-bit
// 0 1
// 0000_ [ B ][ C ]
// 0001_ [ D ][ E ]
// 0010_ [ H ][ L ]
// 0011_ [ W ][ Z ]
// 0100_ [ P ][ C ]
// 0101_ [ S ][ P ]
// 16-bit (ext)
//
// 10000 [ BC ]
// 10010 [ DE ]
// 10100 [ HL ]
// 10110 [ WZ ]
// 11000 [ PC ]
// 11010 [ SP ]
reg[7:0] data[0:11];
reg[15:0] data_out;
wire wr_ext = wr_sel[4];
wire rd_ext = rd_sel[4];
wire[3:0] wr_dst = wr_sel[3:0];
wire[3:0] rd_src = rd_sel[3:0];
localparam EXT_INC = 2'b01;
localparam EXT_DEC = 2'b10;
localparam EXT_INC2 = 2'b11;
always @(posedge clk, posedge rst) begin
if (rst) begin
data[0] <= 8'b0; data[1] <= 8'b0; data[2] <= 8'b0; data[3] <= 8'b0; data[4] <= 8'b0;
data[5] <= 8'b0; data[6] <= 8'b0; data[7] <= 8'b0; data[8] <= 8'b0; data[9] <= 8'b0;
data[10] <= 8'b0; data[11] <= 8'b0;
end else begin
if (ext == EXT_INC) begin
{data[wr_dst], data[wr_dst+1]} <= {data[wr_dst], data[wr_dst+1]} + 1;
end else if (ext == EXT_INC2) begin
{data[wr_dst], data[wr_dst+1]} <= {data[wr_dst], data[wr_dst+1]} + 2;
end else if (ext == EXT_DEC) begin
{data[wr_dst], data[wr_dst+1]} <= {data[wr_dst], data[wr_dst+1]} - 1;
end else if (we) begin
if (wr_ext) begin
{data[wr_dst], data[wr_dst+1]} <= data_in;
end else begin
data[wr_dst] <= data_in[7:0];
end
end
end
end
always @(*) begin
if (rd_ext) begin
data_out = {data[rd_src], data[rd_src+1]};
end else begin
data_out = {8'b0, data[rd_src]};
end
end
endmodule
+153
View File
@@ -0,0 +1,153 @@
module top(
input CLK,
input PIN_24,
output PIN_4,
output PIN_5,
output PIN_6,
output PIN_7,
output PIN_8,
output PIN_9,
output PIN_10,
output PIN_11,
);
assign rst = PIN_24;
assign {PIN_4, PIN_5, PIN_6, PIN_7, PIN_8, PIN_9, PIN_10, PIN_11} = out;
reg[7:0] out;
always @(posedge clk, posedge rst) begin
if (rst) begin
out = 8'b0;
end else if (display) begin
out = alu_out;
end
end
reg[15:0] bus;
always @(*) begin
bus = 16'b0;
if (reg_oe)
bus = reg_out;
else if (mem_oe)
bus = {8'b0, mem_out};
else if (alu_oe)
bus = {8'b0, alu_out};
else if (alu_flags_oe)
bus = {8'b0, alu_flags};
end
reg[23:0] clk_slow;
always @(posedge CLK) begin
clk_slow <= clk_slow + 1;
end
wire rst;
wire hlt;
wire clk;
clock clock(
.hlt(hlt),
.clk_in(clk_slow[14]),
.clk_out(clk)
);
wire[4:0] reg_rd_sel;
wire[4:0] reg_wr_sel;
wire[1:0] reg_ext;
wire reg_oe;
wire reg_we;
wire[15:0] reg_out;
reg_file reg_file(
.clk(clk),
.rst(rst),
.rd_sel(reg_rd_sel),
.wr_sel(reg_wr_sel),
.ext(reg_ext),
.we(reg_we),
.data_in(bus),
.data_out(reg_out)
);
wire mem_mar_we;
wire mem_ram_we;
wire mem_oe;
wire[7:0] mem_out;
memory memory(
.clk(clk),
.rst(rst),
.mar_we(mem_mar_we),
.ram_we(mem_ram_we),
.bus(bus),
.out(mem_out)
);
wire ir_we;
wire[7:0] ir_out;
ir ir(
.clk(clk),
.rst(rst),
.we(ir_we),
.bus(bus[7:0]),
.out(ir_out)
);
wire alu_cs;
wire alu_flags_we;
wire alu_a_we;
wire alu_a_store;
wire alu_a_restore;
wire alu_tmp_we;
wire alu_oe;
wire alu_flags_oe;
wire[4:0] alu_op;
wire[7:0] alu_flags;
wire[7:0] alu_out;
alu alu(
.clk(clk),
.rst(rst),
.cs(alu_cs),
.flags_we(alu_flags_we),
.a_we(alu_a_we),
.a_store(alu_a_store),
.a_restore(alu_a_restore),
.tmp_we(alu_tmp_we),
.op(alu_op),
.bus(bus[7:0]),
.flags(alu_flags),
.out(alu_out)
);
wire display;
controller controller(
.clk(clk),
.rst(rst),
.opcode(ir_out),
.flags(alu_flags),
.out({
display,
hlt,
alu_cs,
alu_flags_we,
alu_a_we,
alu_a_store,
alu_a_restore,
alu_tmp_we,
alu_op,
alu_oe,
alu_flags_oe,
reg_rd_sel,
reg_wr_sel,
reg_ext,
reg_oe,
reg_we,
mem_ram_we,
mem_mar_we,
mem_oe,
ir_we
})
);
endmodule
+154
View File
@@ -0,0 +1,154 @@
module top_tb();
initial begin
$dumpfile("top_tb.vcd");
$dumpvars(0, top_tb);
for (i = 0; i < 12; i++) begin
$dumpvars(0, reg_file.data[i]);
end
for (i = 230; i < 256; i++) begin
$dumpvars(0, memory.ram[i]);
end
rst = 1;
#1 rst = 0;
end
reg clk_in = 0;
integer i;
initial begin
for (i = 0; i < 8096; i++) begin
#1 clk_in = ~clk_in;
end
end
reg[7:0] out;
always @(posedge clk, posedge rst) begin
if (rst) begin
out = 8'b0;
end else if (display) begin
out = alu_out;
end
end
reg[15:0] bus;
always @(*) begin
bus = 16'b0;
if (reg_oe)
bus = reg_out;
else if (mem_oe)
bus = {8'b0, mem_out};
else if (alu_oe)
bus = {8'b0, alu_out};
else if (alu_flags_oe)
bus = {8'b0, alu_flags};
end
reg rst;
wire hlt;
wire clk;
clock clock(
.hlt(hlt),
.clk_in(clk_in),
.clk_out(clk)
);
wire[4:0] reg_rd_sel;
wire[4:0] reg_wr_sel;
wire[1:0] reg_ext;
wire reg_oe;
wire reg_we;
wire[15:0] reg_out;
reg_file reg_file(
.clk(clk),
.rst(rst),
.rd_sel(reg_rd_sel),
.wr_sel(reg_wr_sel),
.ext(reg_ext),
.we(reg_we),
.data_in(bus),
.data_out(reg_out)
);
wire mem_mar_we;
wire mem_ram_we;
wire mem_oe;
wire[7:0] mem_out;
memory memory(
.clk(clk),
.rst(rst),
.mar_we(mem_mar_we),
.ram_we(mem_ram_we),
.bus(bus),
.out(mem_out)
);
wire ir_we;
wire[7:0] ir_out;
ir ir(
.clk(clk),
.rst(rst),
.we(ir_we),
.bus(bus[7:0]),
.out(ir_out)
);
wire alu_cs;
wire alu_flags_we;
wire alu_a_we;
wire alu_a_store;
wire alu_a_restore;
wire alu_tmp_we;
wire alu_oe;
wire alu_flags_oe;
wire[4:0] alu_op;
wire[7:0] alu_flags;
wire[7:0] alu_out;
alu alu(
.clk(clk),
.rst(rst),
.cs(alu_cs),
.flags_we(alu_flags_we),
.a_we(alu_a_we),
.a_store(alu_a_store),
.a_restore(alu_a_restore),
.tmp_we(alu_tmp_we),
.op(alu_op),
.bus(bus[7:0]),
.flags(alu_flags),
.out(alu_out)
);
controller controller(
.clk(clk),
.rst(rst),
.opcode(ir_out),
.flags(alu_flags),
.out({
display,
hlt,
alu_cs,
alu_flags_we,
alu_a_we,
alu_a_store,
alu_a_restore,
alu_tmp_we,
alu_op,
alu_oe,
alu_flags_oe,
reg_rd_sel,
reg_wr_sel,
reg_ext,
reg_oe,
reg_we,
mem_ram_we,
mem_mar_we,
mem_oe,
ir_we
})
);
endmodule