# pyright: reportInvalidTypeForm=false from amaranth import * from amaranth.lib import wiring from buses import PicoBus from isa import * from alu import ALU from decoder import Decoder from regfile import RegisterFile class ExecutionState: INSTR_FETCH = "INSTR_FETCH" EXECUTE = "EXECUTE" LOAD = "LOAD" STORE = "STORE" class CPU(wiring.Component): bus: wiring.Out(PicoBus()) def __init__(self): super().__init__() # TODO: Do prefetch def instr_fetch(self, m, PC): m.d.comb += [ self.bus.mem_valid.eq(1), self.bus.mem_instr.eq(1), self.bus.mem_addr .eq(PC) ] # TODO: Set bus signals to minimize wait states def mem_load(self, m, mem_addr): m.d.comb += [ self.bus.mem_valid.eq(1), self.bus.mem_addr .eq(mem_addr), ] def mem_store(self, m, m_addr, m_data, m_strb): m.d.comb += [ self.bus.mem_valid.eq(1), self.bus.mem_addr .eq(m_addr), self.bus.mem_wdata.eq(m_data), self.bus.mem_wstrb.eq(m_strb) ] def elaborate(self, platform): m = Module() # TODO: Proper debug signals m.submodules.alu = alu = ALU() m.submodules.decoder = self.decoder = decoder = Decoder() m.submodules.regfile = self.regfile = regfile = RegisterFile() PC = Signal(32) instruction = Signal(32) rs1 = Signal(32) rs2 = Signal(32) m.d.comb += [ # Decoder decoder.instruction.eq(instruction), # Regfile regfile.rs1_addr.eq(decoder.rs1), regfile.rs2_addr.eq(decoder.rs2), regfile.rd_addr .eq(decoder.rd), rs1.eq(regfile.rs1_data), rs2.eq(regfile.rs2_data), # ALU alu.value1.eq(rs1), alu.value2.eq(Mux(decoder.alu_op_i, decoder.immediate, rs2)), alu.funct3.eq(decoder.funct3), alu.funct7.eq(decoder.funct7), alu.immediate.eq(decoder.alu_op_i), ] take_branch = Signal(1) with m.Switch(decoder.funct3): with m.Case(Branch.BEQ): m.d.comb += take_branch.eq(rs1 == rs2) with m.Case(Branch.BNE): m.d.comb += take_branch.eq(rs1 != rs2) with m.Case(Branch.BLT): m.d.comb += take_branch.eq(rs1.as_signed() < rs2.as_signed()) with m.Case(Branch.BGE): m.d.comb += take_branch.eq(rs1.as_signed() >= rs2.as_signed()) with m.Case(Branch.BLTU): m.d.comb += take_branch.eq(rs1.as_unsigned() < rs2.as_unsigned()) with m.Case(Branch.BGEU): m.d.comb += take_branch.eq(rs1.as_unsigned() >= rs2.as_unsigned()) next_pc = Signal(32) with m.If((decoder.branch & take_branch) | decoder.jal): m.d.comb += next_pc.eq(PC + decoder.immediate) with m.Elif(decoder.jalr): m.d.comb += next_pc.eq((rs1 + decoder.immediate) & ~Const(1, shape=32)) with m.Else(): m.d.comb += next_pc.eq(PC + 4) mem_addr = Signal(32) # Used for decoding load/store m.d.comb += mem_addr.eq(rs1 + decoder.immediate) store_data = Signal(32) store_strb = Signal(4) with m.FSM() as fsm: with m.State(ExecutionState.INSTR_FETCH): self.instr_fetch(m, PC) with m.If(self.bus.mem_ready): m.d.sync += instruction.eq(self.bus.mem_rdata) m.next = ExecutionState.EXECUTE with m.State(ExecutionState.EXECUTE): with m.If(~decoder.system): m.d.sync += PC.eq(next_pc) with m.If(decoder.load): m.next = ExecutionState.LOAD with m.Elif(decoder.store): m.next = ExecutionState.STORE with m.Else(): m.next = ExecutionState.INSTR_FETCH with m.State(ExecutionState.LOAD): self.mem_load(m, mem_addr) with m.If(self.bus.mem_ready): m.next = ExecutionState.INSTR_FETCH with m.State(ExecutionState.STORE): self.mem_store(m, mem_addr, store_data, store_strb) with m.If(self.bus.mem_ready): m.next = ExecutionState.INSTR_FETCH # Select byte for LB ld_byte = Signal(8) with m.If(mem_addr[1]): with m.If(mem_addr[0]): m.d.comb += ld_byte.eq(self.bus.mem_rdata[24:32]) with m.Else(): m.d.comb += ld_byte.eq(self.bus.mem_rdata[16:24]) with m.Else(): with m.If(mem_addr[0]): m.d.comb += ld_byte.eq(self.bus.mem_rdata[8:16]) with m.Else(): m.d.comb += ld_byte.eq(self.bus.mem_rdata[0:8]) # Select bytes for LH ld_half = Signal(16) with m.If(mem_addr[1]): m.d.comb += ld_half.eq(self.bus.mem_rdata[16:32]) with m.Else(): m.d.comb += ld_half.eq(self.bus.mem_rdata[0:16]) # Load Data Logic load_data = Signal(32) with m.If(decoder.funct3 == Load.LB): m.d.comb += load_data.eq(Cat(ld_byte, ld_byte[7].replicate(24))) with m.Elif(decoder.funct3 == Load.LBU): m.d.comb += load_data.eq(ld_byte) with m.Elif(decoder.funct3 == Load.LH): m.d.comb += load_data.eq(Cat(ld_half, ld_half[15].replicate(16))) with m.Elif(decoder.funct3 == Load.LHU): m.d.comb += load_data.eq(ld_half) with m.Elif(decoder.funct3 == Load.LW): m.d.comb += load_data.eq(self.bus.mem_rdata) # Store Data with m.If(decoder.funct3 == Store.SB): m.d.comb += store_data.eq(rs2[0:8].replicate(4)) with m.Elif(decoder.funct3 == Store.SH): m.d.comb += store_data.eq(rs2[0:16].replicate(2)) with m.Elif(decoder.funct3 == Store.SW): m.d.comb += store_data.eq(rs2) # Store Bit Select with m.If(decoder.funct3 == Store.SB): m.d.comb += store_strb.eq(0b0001 << mem_addr[0:2]) with m.Elif(decoder.funct3 == Store.SH): m.d.comb += store_strb.eq(0b0011 << Cat(Const(0), mem_addr[1])) with m.Elif(decoder.funct3 == Store.SW): m.d.comb += store_strb.eq(0b1111) # Write back writeback_en = Signal(1) writeback_data = Signal(32) with m.If(fsm.ongoing(ExecutionState.EXECUTE)): m.d.comb += writeback_en.eq( decoder.alu_op_r | decoder.alu_op_i | decoder.jal | decoder.jalr | decoder.lui | decoder.auipc ) with m.Elif(fsm.ongoing(ExecutionState.LOAD)): m.d.comb += writeback_en.eq(self.bus.mem_ready) with m.Else(): m.d.comb += writeback_en.eq(0) # Writeback Data with m.If(decoder.load): m.d.comb += writeback_data.eq(load_data) with m.Elif(decoder.jal | decoder.jalr): m.d.comb += writeback_data.eq(PC + 4) with m.Elif(decoder.lui): m.d.comb += writeback_data.eq(decoder.immediate) with m.Elif(decoder.auipc): m.d.comb += writeback_data.eq(PC + decoder.immediate) with m.Else(): m.d.comb += writeback_data.eq(alu.result) m.d.comb += [ regfile.rd_write.eq(writeback_en), regfile.rd_data .eq(writeback_data), ] return m