8-bit Microprocessor Verilog Code -

always #5 clk = ~clk;

// Main control logic always @(posedge clk or posedge rst) begin if (rst) begin pc <= 16'h0000; ir <= 8'h00; state <= FETCH; wr_en <= 1'b0; end else begin case (state) FETCH: begin ir <= data_bus; // Instruction fetch pc <= pc + 1; state <= DECODE; end DECODE: begin wr_en <= 1'b0; case (ir[7:4]) // Opcode in upper nibble 4'b0001: begin // MOV A, B reg_sel_wr <= 2'b00; wr_data <= reg_b; wr_en <= 1'b1; state <= FETCH; end 4'b0010: begin // ADD A, B reg_sel_a <= 2'b00; reg_sel_b <= 2'b01; alu_op <= 3'b000; state <= EXECUTE; end 4'b0011: begin // SUB A, B reg_sel_a <= 2'b00; reg_sel_b <= 2'b01; alu_op <= 3'b001; state <= EXECUTE; end 4'b0100: begin // JMP pc <= ir[3:0], data_bus; state <= FETCH; end 4'b0101: begin // JZ if (zero_flag) pc <= ir[3:0], data_bus; else state <= FETCH; end 4'b0110: begin // LD A, [addr] state <= MEM_READ; end 4'b0111: begin // ST [addr], A state <= MEM_WRITE; end 4'b1000: state <= FETCH; // HLT default: state <= FETCH; endcase end EXECUTE: begin reg_sel_wr <= 2'b00; // Write back to A wr_data <= alu_result; wr_en <= 1'b1; zero_flag <= alu_zero; state <= FETCH; end MEM_READ: begin reg_sel_wr <= 2'b00; wr_data <= data_bus; wr_en <= 1'b1; state <= FETCH; end MEM_WRITE: begin state <= FETCH; end endcase end end endmodule Here's a simple testbench to run a few instructions: 8-bit microprocessor verilog code

reg_file reg_inst (.clk(clk), .rst(rst), .reg_sel_a(reg_sel_a), .reg_sel_b(reg_sel_b), .reg_sel_wr(reg_sel_wr), .wr_data(wr_data), .wr_en(wr_en), .reg_a_out(reg_a), .reg_b_out(reg_b)); always #5 clk = ~clk; // Main control