2018-08-13 22:17:36 +00:00
|
|
|
|
2018-02-15 18:31:32 +00:00
|
|
|
`ifndef ALU_H
|
|
|
|
`define ALU_H
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-02-14 20:58:38 +00:00
|
|
|
// ALU operations
|
2018-02-19 00:19:20 +00:00
|
|
|
`define OP_ZERO 4'h0
|
|
|
|
`define OP_LOAD_A 4'h1
|
2018-09-08 13:59:21 +00:00
|
|
|
`define OP_INC 4'h2
|
|
|
|
`define OP_DEC 4'h3
|
|
|
|
`define OP_ASL 4'h4
|
|
|
|
`define OP_LSR 4'h5
|
|
|
|
`define OP_ROL 4'h6
|
|
|
|
`define OP_ROR 4'h7
|
|
|
|
`define OP_OR 4'h8
|
|
|
|
`define OP_AND 4'h9
|
|
|
|
`define OP_XOR 4'ha
|
2018-02-19 00:19:20 +00:00
|
|
|
`define OP_LOAD_B 4'hb
|
2018-09-08 13:59:21 +00:00
|
|
|
`define OP_ADD 4'hc
|
|
|
|
`define OP_SUB 4'hd
|
|
|
|
`define OP_ADC 4'he
|
|
|
|
`define OP_SBB 4'hf
|
2018-02-15 18:31:32 +00:00
|
|
|
|
2018-09-08 13:59:21 +00:00
|
|
|
// ALU module
|
|
|
|
module ALU(A, B, carry, aluop, Y);
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-09-08 13:59:21 +00:00
|
|
|
parameter N = 8; // default width = 8 bits
|
|
|
|
input [N-1:0] A; // A input
|
|
|
|
input [N-1:0] B; // B input
|
|
|
|
input carry; // carry input
|
|
|
|
input [3:0] aluop; // alu operation
|
|
|
|
output [N:0] Y; // Y output + carry
|
2018-02-03 20:20:56 +00:00
|
|
|
|
|
|
|
always @(*)
|
|
|
|
case (aluop)
|
2018-02-15 18:31:32 +00:00
|
|
|
// unary operations
|
2018-02-19 00:19:20 +00:00
|
|
|
`OP_ZERO: Y = 0;
|
2018-02-15 18:31:32 +00:00
|
|
|
`OP_LOAD_A: Y = {1'b0, A};
|
|
|
|
`OP_INC: Y = A + 1;
|
|
|
|
`OP_DEC: Y = A - 1;
|
|
|
|
// unary operations that generate and/or use carry
|
|
|
|
`OP_ASL: Y = {A, 1'b0};
|
|
|
|
`OP_LSR: Y = {A[0], 1'b0, A[N-1:1]};
|
|
|
|
`OP_ROL: Y = {A, carry};
|
|
|
|
`OP_ROR: Y = {A[0], carry, A[N-1:1]};
|
|
|
|
// binary operations
|
|
|
|
`OP_OR: Y = {1'b0, A | B};
|
|
|
|
`OP_AND: Y = {1'b0, A & B};
|
|
|
|
`OP_XOR: Y = {1'b0, A ^ B};
|
2018-02-19 00:19:20 +00:00
|
|
|
`OP_LOAD_B: Y = {1'b0, B};
|
2018-02-15 18:31:32 +00:00
|
|
|
// binary operations that generate and/or use carry
|
|
|
|
`OP_ADD: Y = A + B;
|
|
|
|
`OP_SUB: Y = A - B;
|
|
|
|
`OP_ADC: Y = A + B + (carry?1:0);
|
|
|
|
`OP_SBB: Y = A - B - (carry?1:0);
|
2018-02-03 20:20:56 +00:00
|
|
|
endcase
|
|
|
|
|
|
|
|
endmodule
|
|
|
|
|
2018-02-09 22:23:14 +00:00
|
|
|
/*
|
|
|
|
Bits Description
|
|
|
|
|
|
|
|
00ddaaaa A @ B -> dest
|
|
|
|
01ddaaaa A @ immediate -> dest
|
|
|
|
11ddaaaa A @ read [B] -> dest
|
|
|
|
10000001 swap A <-> B
|
|
|
|
1001nnnn A -> write [nnnn]
|
|
|
|
1010tttt conditional branch
|
|
|
|
|
|
|
|
dd = destination (00=A, 01=B, 10=IP, 11=none)
|
|
|
|
aaaa = ALU operation (@ operator)
|
|
|
|
nnnn = 4-bit constant
|
|
|
|
tttt = flags test for conditional branch
|
|
|
|
*/
|
|
|
|
|
|
|
|
// destinations for COMPUTE instructions
|
2018-02-15 18:31:32 +00:00
|
|
|
`define DEST_A 2'b00
|
|
|
|
`define DEST_B 2'b01
|
|
|
|
`define DEST_IP 2'b10
|
|
|
|
`define DEST_NOP 2'b11
|
|
|
|
|
2018-02-09 22:23:14 +00:00
|
|
|
// instruction macros
|
2018-02-15 18:31:32 +00:00
|
|
|
`define I_COMPUTE(dest,op) { 2'b00, (dest), (op) }
|
|
|
|
`define I_COMPUTE_IMM(dest,op) { 2'b01, (dest), (op) }
|
|
|
|
`define I_COMPUTE_READB(dest,op) { 2'b11, (dest), (op) }
|
|
|
|
`define I_CONST_IMM_A { 2'b01, `DEST_A, `OP_LOAD_B }
|
|
|
|
`define I_CONST_IMM_B { 2'b01, `DEST_B, `OP_LOAD_B }
|
|
|
|
`define I_JUMP_IMM { 2'b01, `DEST_IP, `OP_LOAD_B }
|
|
|
|
`define I_STORE_A(addr) { 4'b1001, (addr) }
|
|
|
|
`define I_BRANCH_IF(zv,zu,cv,cu) { 4'b1010, (zv), (zu), (cv), (cu) }
|
2018-02-10 06:22:17 +00:00
|
|
|
`define I_CLEAR_CARRY { 8'b10001000 }
|
2018-02-09 22:23:14 +00:00
|
|
|
`define I_SWAP_AB { 8'b10000001 }
|
|
|
|
`define I_RESET { 8'b10111111 }
|
|
|
|
// convenience macros
|
2018-02-15 18:31:32 +00:00
|
|
|
`define I_ZERO_A `I_COMPUTE(`DEST_A, `OP_ZERO)
|
|
|
|
`define I_ZERO_B `I_COMPUTE(`DEST_B, `OP_ZERO)
|
|
|
|
`define I_BRANCH_IF_CARRY(carry) `I_BRANCH_IF(1'b0, 1'b0, carry, 1'b1)
|
|
|
|
`define I_BRANCH_IF_ZERO(zero) `I_BRANCH_IF(zero, 1'b1, 1'b0, 1'b0)
|
|
|
|
`define I_CLEAR_ZERO `I_COMPUTE(`DEST_NOP, `OP_ZERO)
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-02-10 06:22:17 +00:00
|
|
|
module CPU(clk, reset, address, data_in, data_out, write);
|
2018-02-15 18:31:32 +00:00
|
|
|
|
2018-02-10 06:22:17 +00:00
|
|
|
input clk;
|
|
|
|
input reset;
|
|
|
|
output [7:0] address;
|
|
|
|
input [7:0] data_in;
|
|
|
|
output [7:0] data_out;
|
|
|
|
output write;
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-02-05 20:51:20 +00:00
|
|
|
reg [7:0] IP;
|
2018-02-03 20:20:56 +00:00
|
|
|
reg [7:0] A, B;
|
|
|
|
reg [8:0] Y;
|
|
|
|
reg [2:0] state;
|
|
|
|
|
|
|
|
reg carry;
|
|
|
|
reg zero;
|
|
|
|
wire [1:0] flags = { zero, carry };
|
2018-02-05 20:51:20 +00:00
|
|
|
|
|
|
|
reg [7:0] opcode;
|
|
|
|
wire [3:0] aluop = opcode[3:0];
|
|
|
|
wire [1:0] opdest = opcode[5:4];
|
2018-02-09 22:23:14 +00:00
|
|
|
wire B_or_data = opcode[6];
|
2018-02-05 20:51:20 +00:00
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
localparam S_RESET = 0;
|
|
|
|
localparam S_SELECT = 1;
|
|
|
|
localparam S_DECODE = 2;
|
2018-02-07 00:07:40 +00:00
|
|
|
localparam S_COMPUTE = 3;
|
|
|
|
localparam S_READ_IP = 4;
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-02-10 06:22:17 +00:00
|
|
|
ALU alu(
|
|
|
|
.A(A),
|
|
|
|
.B(B_or_data ? data_in : B),
|
|
|
|
.Y(Y),
|
|
|
|
.aluop(aluop),
|
|
|
|
.carry(carry));
|
2018-02-03 20:20:56 +00:00
|
|
|
|
|
|
|
always @(posedge clk)
|
|
|
|
if (reset) begin
|
|
|
|
state <= 0;
|
|
|
|
write <= 0;
|
|
|
|
end else begin
|
|
|
|
case (state)
|
|
|
|
// state 0: reset
|
|
|
|
S_RESET: begin
|
2018-02-05 20:51:20 +00:00
|
|
|
IP <= 8'h80;
|
2018-02-03 20:20:56 +00:00
|
|
|
write <= 0;
|
|
|
|
state <= S_SELECT;
|
|
|
|
end
|
|
|
|
// state 1: select opcode address
|
|
|
|
S_SELECT: begin
|
2018-02-05 20:51:20 +00:00
|
|
|
address <= IP;
|
|
|
|
IP <= IP + 1;
|
2018-02-03 20:20:56 +00:00
|
|
|
write <= 0;
|
|
|
|
state <= S_DECODE;
|
|
|
|
end
|
|
|
|
// state 2: read/decode opcode
|
|
|
|
S_DECODE: begin
|
2018-02-10 06:22:17 +00:00
|
|
|
opcode <= data_in; // (only use opcode next cycle)
|
2018-02-03 20:20:56 +00:00
|
|
|
casez (data_in)
|
2018-02-05 20:51:20 +00:00
|
|
|
// ALU A + B -> dest
|
2018-02-03 20:20:56 +00:00
|
|
|
8'b00??????: begin
|
2018-02-05 20:51:20 +00:00
|
|
|
state <= S_COMPUTE;
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2018-02-05 20:51:20 +00:00
|
|
|
// ALU A + immediate -> dest
|
|
|
|
8'b01??????: begin
|
|
|
|
address <= IP;
|
|
|
|
IP <= IP + 1;
|
2018-02-07 00:07:40 +00:00
|
|
|
state <= S_COMPUTE;
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2018-02-09 22:23:14 +00:00
|
|
|
// ALU A + read [B] -> dest
|
2018-02-07 00:07:40 +00:00
|
|
|
8'b11??????: begin
|
2018-02-05 20:51:20 +00:00
|
|
|
address <= B;
|
2018-02-07 00:07:40 +00:00
|
|
|
state <= S_COMPUTE;
|
2018-02-05 20:51:20 +00:00
|
|
|
end
|
2018-02-10 06:22:17 +00:00
|
|
|
// A -> write [nnnn]
|
2018-02-09 22:23:14 +00:00
|
|
|
8'b1001????: begin
|
2018-02-10 06:22:17 +00:00
|
|
|
address <= {4'b0, data_in[3:0]};
|
2018-02-07 00:07:40 +00:00
|
|
|
data_out <= A;
|
|
|
|
write <= 1;
|
|
|
|
state <= S_SELECT;
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
2018-02-09 22:23:14 +00:00
|
|
|
// swap A,B
|
|
|
|
8'b10000001: begin
|
|
|
|
A <= B;
|
|
|
|
B <= A;
|
|
|
|
state <= S_SELECT;
|
|
|
|
end
|
2018-02-07 00:07:40 +00:00
|
|
|
// conditional branch
|
2018-02-09 22:23:14 +00:00
|
|
|
8'b1010????: begin
|
2018-02-07 00:07:40 +00:00
|
|
|
if (
|
|
|
|
(data_in[0] && (data_in[1] == carry)) ||
|
|
|
|
(data_in[2] && (data_in[3] == zero)))
|
|
|
|
begin
|
|
|
|
address <= IP;
|
|
|
|
state <= S_READ_IP;
|
|
|
|
end else begin
|
|
|
|
state <= S_SELECT;
|
|
|
|
end
|
|
|
|
IP <= IP + 1; // skip immediate
|
|
|
|
end
|
2018-02-05 20:51:20 +00:00
|
|
|
// fall-through RESET
|
2018-02-03 20:20:56 +00:00
|
|
|
default: begin
|
|
|
|
state <= S_RESET; // reset
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
2018-02-07 00:07:40 +00:00
|
|
|
// state 3: compute ALU op and flags
|
2018-02-03 20:20:56 +00:00
|
|
|
S_COMPUTE: begin
|
2018-02-07 00:07:40 +00:00
|
|
|
// transfer ALU output to destination
|
2018-02-05 20:51:20 +00:00
|
|
|
case (opdest)
|
2018-02-15 18:31:32 +00:00
|
|
|
`DEST_A: A <= Y[7:0];
|
|
|
|
`DEST_B: B <= Y[7:0];
|
|
|
|
`DEST_IP: IP <= Y[7:0];
|
|
|
|
`DEST_NOP: ;
|
2018-02-05 20:51:20 +00:00
|
|
|
endcase
|
2018-02-15 18:31:32 +00:00
|
|
|
// set carry for certain operations (4-7,12-15)
|
|
|
|
if (aluop[2]) carry <= Y[8];
|
2018-02-07 00:07:40 +00:00
|
|
|
// set zero flag
|
2018-02-10 06:22:17 +00:00
|
|
|
zero <= ~|Y[7:0];
|
2018-02-07 00:07:40 +00:00
|
|
|
// repeat CPU loop
|
|
|
|
state <= S_SELECT;
|
|
|
|
end
|
|
|
|
// state 4: read new IP from memory (immediate mode)
|
|
|
|
S_READ_IP: begin
|
|
|
|
IP <= data_in;
|
2018-02-03 20:20:56 +00:00
|
|
|
state <= S_SELECT;
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|
|
|
|
|
2018-02-21 19:32:11 +00:00
|
|
|
`ifdef TOPMOD__test_CPU_top
|
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
module test_CPU_top(
|
|
|
|
input clk,
|
|
|
|
input reset,
|
|
|
|
output [7:0] address_bus,
|
|
|
|
output reg [7:0] to_cpu,
|
|
|
|
output [7:0] from_cpu,
|
2018-02-05 20:51:20 +00:00
|
|
|
output write_enable,
|
|
|
|
output [7:0] IP,
|
|
|
|
output [7:0] A,
|
2018-02-10 06:22:17 +00:00
|
|
|
output [7:0] B,
|
|
|
|
output zero,
|
|
|
|
output carry
|
2018-02-03 20:20:56 +00:00
|
|
|
);
|
|
|
|
|
2018-02-09 22:23:14 +00:00
|
|
|
reg [7:0] ram[0:127];
|
|
|
|
reg [7:0] rom[0:127];
|
2018-02-03 20:20:56 +00:00
|
|
|
|
2018-02-05 20:51:20 +00:00
|
|
|
assign IP = cpu.IP;
|
|
|
|
assign A = cpu.A;
|
|
|
|
assign B = cpu.B;
|
2018-02-10 06:22:17 +00:00
|
|
|
assign zero = cpu.zero;
|
|
|
|
assign carry = cpu.carry;
|
2018-02-05 20:51:20 +00:00
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
CPU cpu(.clk(clk),
|
|
|
|
.reset(reset),
|
|
|
|
.address(address_bus),
|
|
|
|
.data_in(to_cpu),
|
|
|
|
.data_out(from_cpu),
|
|
|
|
.write(write_enable));
|
|
|
|
|
2018-02-10 06:22:17 +00:00
|
|
|
always @(posedge clk)
|
|
|
|
if (write_enable) begin
|
|
|
|
ram[address_bus[6:0]] <= from_cpu;
|
|
|
|
end
|
|
|
|
|
2018-02-03 20:20:56 +00:00
|
|
|
always @(*)
|
2018-02-10 06:22:17 +00:00
|
|
|
if (address_bus[7] == 0)
|
2018-02-03 20:20:56 +00:00
|
|
|
to_cpu = ram[address_bus[6:0]];
|
|
|
|
else
|
|
|
|
to_cpu = rom[address_bus[6:0]];
|
|
|
|
|
|
|
|
initial begin
|
2018-02-21 19:32:11 +00:00
|
|
|
`ifdef EXT_INLINE_ASM
|
|
|
|
// example code: Fibonacci sequence
|
2018-02-09 22:23:14 +00:00
|
|
|
rom = '{
|
2018-02-21 19:32:11 +00:00
|
|
|
__asm
|
|
|
|
|
|
|
|
.arch femto8
|
|
|
|
.org 128
|
|
|
|
.len 128
|
|
|
|
|
|
|
|
Start:
|
|
|
|
zero A ; A <= 0
|
|
|
|
ldb #1 ; B <= 1
|
|
|
|
Loop:
|
|
|
|
add A,B ; A <= A + B
|
|
|
|
swapab ; swap A,B
|
|
|
|
bcc Loop ; repeat until carry set
|
|
|
|
reset ; end of loop; reset CPU
|
|
|
|
|
|
|
|
__endasm
|
2018-02-09 22:23:14 +00:00
|
|
|
};
|
2018-02-21 19:32:11 +00:00
|
|
|
`endif
|
2018-02-03 20:20:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|
2018-02-15 18:31:32 +00:00
|
|
|
|
|
|
|
`endif
|
2018-02-21 19:32:11 +00:00
|
|
|
|
|
|
|
`endif
|