2021-02-15 23:55:09 +01:00
|
|
|
`include "cpu_pkg.svh"
|
|
|
|
|
|
|
|
import cpu_pkg::*;
|
|
|
|
|
|
|
|
module cpu (
|
|
|
|
input logic clk_i,
|
|
|
|
input logic nreset_i,
|
|
|
|
|
|
|
|
output logic [15:0] address_o,
|
2021-02-19 00:22:26 +01:00
|
|
|
input logic [ 7:0] rdata_i,
|
|
|
|
output logic we_o,
|
|
|
|
output logic [ 7:0] wdata_o
|
2021-02-15 23:55:09 +01:00
|
|
|
);
|
|
|
|
|
2021-02-17 00:05:46 +01:00
|
|
|
state_t state;
|
|
|
|
|
|
|
|
logic alu_op_valid;
|
|
|
|
alu_op_t alu_op;
|
|
|
|
op_src_t op_src;
|
|
|
|
op_dest_t op_dest;
|
2021-02-19 00:22:26 +01:00
|
|
|
adr_src_t adr_src;
|
2021-02-20 23:57:15 +01:00
|
|
|
logic reg_write_alu;
|
2021-02-17 00:05:46 +01:00
|
|
|
|
|
|
|
logic [ 7:0] alu_operand;
|
|
|
|
|
|
|
|
logic [ 7:0] rega;
|
|
|
|
logic [ 7:0] regf;
|
2021-02-15 23:55:09 +01:00
|
|
|
|
|
|
|
logic [15:0] pc;
|
2021-02-17 00:05:46 +01:00
|
|
|
logic [15:0] sp;
|
2021-02-19 00:22:26 +01:00
|
|
|
logic [15:0] hl;
|
2021-02-15 23:55:09 +01:00
|
|
|
|
2021-02-17 23:40:24 +01:00
|
|
|
logic instr_valid;
|
|
|
|
logic instr_undef;
|
|
|
|
|
|
|
|
logic [ 7:0] operand8;
|
|
|
|
logic [15:0] operand16;
|
|
|
|
|
|
|
|
reg8_t reg8_wselect;
|
|
|
|
logic reg8_we;
|
|
|
|
logic [ 7:0] reg8_wdata;
|
|
|
|
reg16_t reg16_wselect;
|
|
|
|
logic reg16_we;
|
|
|
|
logic [15:0] reg16_wdata;
|
|
|
|
reg8_t reg8_rselect;
|
|
|
|
reg16_t reg16_rselect;
|
2021-02-19 00:22:26 +01:00
|
|
|
reg16_t reg16_rselect2;
|
2021-02-17 23:40:24 +01:00
|
|
|
logic [ 7:0] reg8_rdata;
|
|
|
|
logic [15:0] reg16_rdata;
|
2021-02-19 00:22:26 +01:00
|
|
|
logic [15:0] reg16_rdata2;
|
|
|
|
alu16_op_t alu16_op;
|
2021-02-20 23:57:15 +01:00
|
|
|
logic [ 7:0] alu_out8;
|
2021-02-19 00:22:26 +01:00
|
|
|
logic [15:0] alu_out16;
|
2021-02-17 23:40:24 +01:00
|
|
|
|
2021-02-15 23:55:09 +01:00
|
|
|
control control_inst (
|
2021-02-17 00:05:46 +01:00
|
|
|
.clk_i (clk_i),
|
|
|
|
.nreset_i (nreset_i),
|
|
|
|
.rdata_i (rdata_i),
|
|
|
|
.state_o (state),
|
2021-02-20 16:00:01 +01:00
|
|
|
.f_i (regf),
|
2021-02-17 00:05:46 +01:00
|
|
|
.pc_o (pc),
|
|
|
|
.sp_o (sp),
|
|
|
|
.alu_op_valid_o(alu_op_valid),
|
|
|
|
.alu_op_o (alu_op),
|
2021-02-20 23:57:15 +01:00
|
|
|
.reg_write_alu_o(reg_write_alu),
|
2021-02-17 00:05:46 +01:00
|
|
|
.op_src_o (op_src),
|
2021-02-17 23:40:24 +01:00
|
|
|
.op_dest_o (op_dest),
|
|
|
|
.instr_valid_o (instr_valid),
|
|
|
|
.instr_undef_o (instr_undef),
|
|
|
|
.operand8_o (operand8),
|
|
|
|
.operand16_o (operand16),
|
2021-02-20 16:56:22 +01:00
|
|
|
.reg8_dest_o (reg8_wselect),
|
2021-02-19 22:53:13 +01:00
|
|
|
.reg8_src_o (reg8_rselect),
|
2021-02-19 00:22:26 +01:00
|
|
|
.alu16_op_o (alu16_op),
|
|
|
|
.reg16_src_o (reg16_rselect),
|
|
|
|
.reg16_dest_o (reg16_wselect),
|
|
|
|
.memory_we_o (we_o),
|
|
|
|
.adr_src_o (adr_src)
|
2021-02-17 00:05:46 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
alu alu_inst (
|
|
|
|
.clk_i (clk_i),
|
|
|
|
.nreset_i (nreset_i),
|
|
|
|
.alu_op_valid_i(alu_op_valid),
|
|
|
|
.alu_op_i (alu_op),
|
|
|
|
.operand_i (alu_operand),
|
2021-02-19 22:53:13 +01:00
|
|
|
.inx8_i (operand8[5:3]), // Used for bit/set/res
|
2021-02-17 00:05:46 +01:00
|
|
|
.a_o (rega),
|
2021-02-19 00:22:26 +01:00
|
|
|
.f_o (regf),
|
|
|
|
.alu16_op_i (alu16_op),
|
|
|
|
.inx16_i (reg16_rdata2),
|
|
|
|
.iny16_i (reg16_rdata),
|
2021-02-20 23:57:15 +01:00
|
|
|
.out8_o (alu_out8),
|
2021-02-19 00:22:26 +01:00
|
|
|
.out16_o (alu_out16)
|
2021-02-15 23:55:09 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
registers registers_inst (
|
|
|
|
.clk_i (clk_i),
|
2021-02-17 23:40:24 +01:00
|
|
|
.nreset_i(nreset_i),
|
|
|
|
|
|
|
|
.reg8_wselect_i (reg8_wselect),
|
|
|
|
.reg8_we_i (reg8_we),
|
|
|
|
.reg8_wdata_i (reg8_wdata),
|
|
|
|
.reg16_wselect_i(reg16_wselect),
|
|
|
|
.reg16_we_i (reg16_we),
|
|
|
|
.reg16_wdata_i (reg16_wdata),
|
|
|
|
.reg8_rselect_i (reg8_rselect),
|
|
|
|
.reg8_rdata_o (reg8_rdata),
|
|
|
|
.reg16_rselect_i(reg16_rselect),
|
2021-02-19 00:22:26 +01:00
|
|
|
.reg16_rdata_o (reg16_rdata),
|
|
|
|
.reg16_rselect2_i(reg16_rselect2),
|
|
|
|
.reg16_rdata2_o (reg16_rdata2),
|
|
|
|
.hl_o (hl)
|
2021-02-15 23:55:09 +01:00
|
|
|
);
|
|
|
|
|
2021-02-19 22:53:13 +01:00
|
|
|
assign alu_operand = (op_src == OP_SRC_A) ? rega :
|
|
|
|
(op_src == OP_SRC_OPERAND8) ? operand8 :
|
|
|
|
(op_src == OP_SRC_REG8) ? reg8_rdata :
|
2021-02-22 22:42:40 +01:00
|
|
|
(op_src == OP_SRC_MEMORY) ? rdata_i :
|
2021-02-20 16:56:22 +01:00
|
|
|
'X;
|
2021-02-17 00:05:46 +01:00
|
|
|
|
2021-02-20 16:56:22 +01:00
|
|
|
assign reg8_we = instr_valid & (op_dest == OP_DEST_REG8);
|
2021-02-20 23:57:15 +01:00
|
|
|
assign reg8_wdata = reg_write_alu ? alu_out8 : alu_operand;
|
2021-02-19 00:22:26 +01:00
|
|
|
assign reg16_we = instr_valid & (op_dest == OP_DEST_REG16);
|
|
|
|
assign reg16_wdata = (op_src == OP_SRC_OPERAND16) ? operand16 : alu_out16;
|
|
|
|
assign reg16_rselect2 = reg16_wselect;
|
2021-02-17 23:40:24 +01:00
|
|
|
|
2021-02-22 22:42:40 +01:00
|
|
|
assign address_o = (adr_src == ADR_SRC_HL) ? hl :
|
|
|
|
(adr_src == ADR_SRC_REG8) ? {8'hFF, reg8_rdata} :
|
|
|
|
(adr_src == ADR_SRC_REG16) ? reg16_rdata :
|
2021-02-22 22:58:54 +01:00
|
|
|
(adr_src == ADR_SRC_OPERAND8) ? {8'hFF, operand8} :
|
2021-02-22 22:42:40 +01:00
|
|
|
(adr_src == ADR_SRC_OPERAND16) ? operand16 :
|
|
|
|
pc;
|
2021-02-20 16:56:22 +01:00
|
|
|
assign wdata_o = (op_dest == OP_DEST_MEMORY) ? alu_operand :
|
|
|
|
rega; // ldi/ldd hl, a use the normal control paths for HL
|
2021-02-15 23:55:09 +01:00
|
|
|
|
|
|
|
endmodule : cpu
|