// Original Coco 3 Plus contributions: Copyright (c) 2026 Brian K. Holman. // Original component notices/terms retained; see COPYRIGHT.md and LICENSING.md. // SPDX-License-Identifier: BSD-3-Clause // TMS7000 instruction engine, behavioral port of VCCPlus a8a84f22 / MAME. // Copyright/provenance: hap, Tim Lindner; original notices in LICENSE. // All state uses clk. One step executes one instruction OR interrupt entry. // cycles reports architectural icount (/2 input clock), NOT fabric clocks. // The future scheduler must pace steps/timer ticks; no chip clock is generated. // External memory supplies RF, peripheral file and ROM with a held-request bus. // Reset does the CPU reset TRAP; GPIO reset callbacks belong to the wrapper. module ssc_tms7000_exec(input wire clk,reset,restart,step, input wire irq_valid,input wire [1:0] irq_index, output reg irq_ack,output reg initialized,done, output reg [15:0] pc,output reg [7:0] sp,status, output reg idle,output reg [31:0] illegal_count,output reg [6:0] cycles, output reg mem_request,mem_write,mem_peripheral,output reg [15:0] mem_address, output reg [7:0] mem_write_data,input wire mem_ready,input wire [7:0] mem_read_data); `include "ssc_tms7000_defs.vh" localparam [6:0] RESET_START=0,WAIT_STEP=1,FETCHED=2,DECODE=3, SRC_INDEX=4,SRC_VALUE=5,DEST_START=6,DEST_INDEX=7,DEST_VALUE=8,ALU=9, UNARY_START=10,UNARY_INDEX=11,PAIR_HIGH=12,PAIR_LOW=13,XCH_VALUE=14, WRITE_RESULT=15,WRITE_PAIR_LOW=16,BRANCH_START=17,BRANCH_VALUE=18, ADDRESS_START=19,ADDRESS_HIGH=20,ADDRESS_LOW=21,POINTER_INDEX=22, POINTER_HIGH=23,POINTER_LOW=24,INDEX_VALUE=25,ADDRESS_READY=26, MOVD_INDEX=27,LOAD_VALUE=28,STORE_VALUE=29,CMP_VALUE=30, PUSH_HIGH=31,PUSH_LOW=32,AFTER_PUSH=33,VECTOR_HIGH=34,VECTOR_LOW=35, POP_VALUE=36,RETURN_LOW=37,RETURN_HIGH=38,RETURN_STATUS=39, LDSP_VALUE=40,FINISH=41,WAIT_RELEASE=42,BUS_WAIT=43,BUS_RELEASE=44, IRQ_PUSH_PC=45,ALU_COMMIT=46,MULTIPLY_COMMIT=47,DECIMAL_WAIT=48,DECIMAL_COMMIT=49, DISPATCH=50; reg [6:0] state,return_state; reg [7:0] opcode,value,source_value,destination_value,high_byte,low_byte; reg [5:0] operation; reg [4:0] mode; reg [15:0] destination_address,effective_address,pair_value,return_pc,vector_address; reg [7:0] pointer_index; reg branch_taken,is_reset,is_interrupt,destination_peripheral; reg [15:0] product,saved_result; reg [7:0] saved_status; reg saved_write,saved_branch; reg [5:0] saved_extra; wire [5:0] decoded_operation,decoded_cycles; wire [4:0] decoded_mode; ssc_tms7000_decode decoder(opcode,decoded_operation,decoded_mode,decoded_cycles); wire [15:0] alu_result; wire [7:0] alu_status; wire alu_write,alu_branch; wire [5:0] alu_extra; wire [7:0] decimal_add,decimal_sub; wire decimal_add_carry,decimal_sub_carry; wire [7:0] decimal_result=(operation==OP_DAC)?decimal_add:decimal_sub; ssc_tms7000_bcd decimal_unit(clk,reset,destination_value,source_value,status[7], decimal_add,decimal_sub,decimal_add_carry,decimal_sub_carry); ssc_tms7000_alu arithmetic(operation, (operation==OP_DECD)?pair_value:{8'd0,destination_value},source_value,status, alu_result,alu_status,alu_write,alu_branch,alu_extra); task read_memory(input [15:0] address,input [6:0] continuation); begin mem_request<=1;mem_write<=0;mem_peripheral<=0;mem_address<=address;mem_write_data<=0; return_state<=continuation;state<=BUS_WAIT;end endtask task write_memory(input [15:0] address,input [7:0] data,input [6:0] continuation); begin mem_request<=1;mem_write<=1;mem_peripheral<=0;mem_address<=address;mem_write_data<=data; return_state<=continuation;state<=BUS_WAIT;end endtask task immediate(input [6:0] continuation); begin read_memory(pc,continuation);pc<=pc+16'd1;end endtask function [15:0] previous_register(input [15:0] address); begin previous_register={8'd0,(address[7:0]-8'd1)};end endfunction always @(posedge clk) begin if(reset) begin state<=RESET_START;return_state<=WAIT_STEP;mem_request<=0;mem_write<=0;mem_peripheral<=0; mem_address<=0;mem_write_data<=0;initialized<=0;done<=0;irq_ack<=0; pc<=0;sp<=8'hff;status<=0;idle<=0;illegal_count<=0;cycles<=0; opcode<=0;value<=0;source_value<=0;destination_value<=0;high_byte<=0;low_byte<=0; operation<=OP_BAD;mode<=AM_NONE;destination_address<=0;effective_address<=0; pair_value<=0;return_pc<=0;vector_address<=0;pointer_index<=0; branch_taken<=0;is_reset<=1;is_interrupt<=0;destination_peripheral<=0; product<=0;saved_result<=0;saved_status<=0;saved_write<=0;saved_branch<=0;saved_extra<=0; end else begin irq_ack<=0; // No operation-dependent operand mux before the physical multiplier. product<=destination_value*source_value; case(state) RESET_START:begin initialized<=0;done<=0; return_pc<=pc+{15'd0,idle};sp<=8'hff;status<=0;idle<=0; opcode<=8'hff;vector_address<=16'hfffe;is_reset<=1;is_interrupt<=0; cycles<=17;state<=PUSH_HIGH; end WAIT_STEP:begin done<=0; if(restart) state<=RESET_START; else if(step) begin is_reset<=0;is_interrupt<=0; if(irq_valid && status[4] && irq_index<3) begin irq_ack<=1;is_interrupt<=1;cycles<=idle?7'd17:7'd19; vector_address<=16'hfffc-{13'd0,irq_index,1'b0}; return_pc<=pc+{15'd0,idle};idle<=0; sp<=sp+8'd1;write_memory({8'd0,(sp+8'd1)},status,IRQ_PUSH_PC);status<=0; end else immediate(FETCHED); end end FETCHED:begin opcode<=value;state<=DECODE;end DECODE:begin operation<=decoded_operation;mode<=decoded_mode;cycles<={1'b0,decoded_cycles}; branch_taken<=0;destination_peripheral<=0; state<=DISPATCH; end DISPATCH:begin case(mode) AM_R_A,AM_R_B,AM_R_R,AM_P_A,AM_P_B:immediate(SRC_INDEX); AM_I_A,AM_I_B,AM_I_R,AM_I_P:immediate(SRC_VALUE); AM_A_A,AM_A_B,AM_A_R,AM_A_P:read_memory(0,SRC_VALUE); AM_B_A,AM_B_B,AM_B_R,AM_B_P:read_memory(1,SRC_VALUE); AM_A,AM_B,AM_R:state<=UNARY_START; AM_DIR,AM_IND,AM_INX:state<=ADDRESS_START; default:case(operation) OP_BAD:begin illegal_count<=illegal_count+1'b1;state<=FINISH;end OP_NOP:state<=FINISH; OP_IDLE:begin pc<=pc-16'd1;idle<=1;state<=FINISH;end OP_EINT:begin status<=status|8'hf0;state<=FINISH;end OP_DINT:begin status<=status&8'h0f;state<=FINISH;end OP_SETC:begin status<=(status&8'hbf)|8'ha0;state<=FINISH;end OP_STSP:write_memory(1,sp,FINISH); OP_LDSP:read_memory(1,LDSP_VALUE); OP_PUSHST:begin sp<=sp+8'd1;write_memory({8'd0,(sp+8'd1)},status,FINISH);end OP_POPST:begin read_memory({8'd0,sp},RETURN_STATUS);sp<=sp-8'd1;end OP_RETS,OP_RETI:begin read_memory({8'd0,sp},RETURN_LOW);sp<=sp-8'd1;end OP_TRAP:begin return_pc<=pc;vector_address<={8'hff,opcode[6:0],1'b0};state<=PUSH_HIGH; end OP_JUMP:begin case(opcode[2:0]) 0:branch_taken<=1;1:branch_taken<=status[6];2:branch_taken<=status[5];3:branch_taken<=status[7]; 4:branch_taken<=!(status[5]||status[6]);5:branch_taken<=!status[6];6:branch_taken<=!status[5];7:branch_taken<=!status[7]; endcase state<=BRANCH_START; end default:state<=FINISH; endcase endcase end SRC_INDEX:begin read_memory((mode==AM_P_A||mode==AM_P_B)?{8'h01,value}:{8'd0,value},SRC_VALUE); mem_peripheral<=(mode==AM_P_A||mode==AM_P_B); end SRC_VALUE:begin source_value<=value;state<=DEST_START;end DEST_START:case(mode) AM_R_A,AM_I_A,AM_B_A,AM_P_A,AM_A_A:begin destination_address<=0;read_memory(0,DEST_VALUE);end AM_R_B,AM_I_B,AM_P_B,AM_A_B,AM_B_B:begin destination_address<=1;read_memory(1,DEST_VALUE);end default:immediate(DEST_INDEX); endcase DEST_INDEX:begin destination_address<=(mode==AM_A_P||mode==AM_B_P||mode==AM_I_P)?{8'h01,value}:{8'd0,value}; destination_peripheral<=(mode==AM_A_P||mode==AM_B_P||mode==AM_I_P); read_memory((mode==AM_A_P||mode==AM_B_P||mode==AM_I_P)?{8'h01,value}:{8'd0,value},DEST_VALUE); mem_peripheral<=(mode==AM_A_P||mode==AM_B_P||mode==AM_I_P); end DEST_VALUE:begin destination_value<=value;state<=ALU;end UNARY_START:begin if(mode==AM_R) immediate(UNARY_INDEX); else begin destination_address<=(mode==AM_A)?16'd0:16'd1;state<=UNARY_INDEX;value<=(mode==AM_A)?8'd0:8'd1;end end UNARY_INDEX:begin destination_address<={8'd0,value}; if(operation==OP_POP) begin read_memory({8'd0,sp},POP_VALUE);sp<=sp-8'd1;end else if(operation==OP_DECD) read_memory({8'd0,(value-8'd1)},PAIR_HIGH); else read_memory({8'd0,value},(operation==OP_PUSH)?STORE_VALUE:DEST_VALUE); end PAIR_HIGH:begin high_byte<=value;read_memory(destination_address,PAIR_LOW);end PAIR_LOW:begin pair_value<={high_byte,value};state<=ALU;end XCH_VALUE:begin source_value<=value;state<=ALU;end ALU:begin if(operation==OP_XCHB && mode!=AM_NONE) begin mode<=AM_NONE;read_memory(1,XCH_VALUE); end else if(operation==OP_MPY) state<=MULTIPLY_COMMIT; else if(operation==OP_DAC||operation==OP_DSB) state<=DECIMAL_WAIT; else begin saved_result<=alu_result;saved_status<=alu_status;saved_write<=alu_write; saved_branch<=alu_branch;saved_extra<=alu_extra;state<=ALU_COMMIT; end end MULTIPLY_COMMIT:begin status<={1'b0,product[15],(product[15:8]==0),status[4:0]}; pair_value<=product;cycles<=cycles+7'd39;destination_address<=1; write_memory(0,product[15:8],WRITE_PAIR_LOW); end DECIMAL_WAIT:state<=DECIMAL_COMMIT; DECIMAL_COMMIT:begin status<={((operation==OP_DAC)?decimal_add_carry:decimal_sub_carry), decimal_result[7],(decimal_result==0),status[4:0]}; cycles<=cycles+7'd2; write_memory(destination_address,decimal_result,FINISH);mem_peripheral<=destination_peripheral; end ALU_COMMIT:begin status<=saved_status;pair_value<=saved_result;branch_taken<=saved_branch; cycles<=cycles+{1'b0,saved_extra}; if(operation==OP_DECD) write_memory(previous_register(destination_address),saved_result[15:8],WRITE_PAIR_LOW); else if(operation==OP_XCHB) write_memory(1,destination_value,WRITE_RESULT); // DJNZ consumes displacement BEFORE writing its decremented operand. // This matters when PC wraps into RF and the two addresses alias. else if(operation==OP_DJNZ) state<=BRANCH_START; else if(saved_write) begin write_memory(destination_address,saved_result[7:0],BRANCH_START);mem_peripheral<=destination_peripheral;end else state<=BRANCH_START; end WRITE_RESULT:begin write_memory(destination_address,pair_value[7:0],BRANCH_START);mem_peripheral<=destination_peripheral;end WRITE_PAIR_LOW:write_memory(destination_address,pair_value[7:0],BRANCH_START); BRANCH_START:begin if(operation==OP_JUMP) begin cycles<=cycles+(branch_taken?7'd4:7'd2);immediate(BRANCH_VALUE);end else if(operation==OP_BTJO||operation==OP_BTJZ||operation==OP_DJNZ) immediate(BRANCH_VALUE); else state<=FINISH; end BRANCH_VALUE:begin if(branch_taken) pc<=pc+{{8{value[7]}},value}; if(operation==OP_DJNZ) write_memory(destination_address,pair_value[7:0],FINISH); else state<=FINISH; end ADDRESS_START:begin if(mode==AM_IND) immediate(POINTER_INDEX);else immediate(ADDRESS_HIGH);end ADDRESS_HIGH:begin high_byte<=value;immediate(ADDRESS_LOW);end ADDRESS_LOW:begin effective_address<={high_byte,value}; if(mode==AM_INX) read_memory(1,INDEX_VALUE);else state<=ADDRESS_READY; end POINTER_INDEX:begin pointer_index<=value;read_memory({8'd0,(value-8'd1)},POINTER_HIGH);end POINTER_HIGH:begin high_byte<=value;read_memory({8'd0,pointer_index},POINTER_LOW);end POINTER_LOW:begin effective_address<={high_byte,value};state<=ADDRESS_READY;end INDEX_VALUE:begin effective_address<=effective_address+{8'd0,value};state<=ADDRESS_READY;end ADDRESS_READY:case(operation) OP_MOVD:immediate(MOVD_INDEX); OP_LDA:read_memory(effective_address,LOAD_VALUE); OP_STA:read_memory(0,STORE_VALUE); OP_CMPA:read_memory(effective_address,CMP_VALUE); OP_BR:begin pc<=effective_address;state<=FINISH;end OP_CALL:begin return_pc<=pc;state<=PUSH_HIGH;end default:state<=FINISH; endcase MOVD_INDEX:begin destination_address<={8'd0,value};pair_value<=effective_address; status<={1'b0,effective_address[15],(effective_address[15:8]==0),status[4:0]}; write_memory({8'd0,(value-8'd1)},effective_address[15:8],WRITE_PAIR_LOW); end LOAD_VALUE:begin status<={1'b0,value[7],(value==0),status[4:0]};write_memory(0,value,FINISH); end STORE_VALUE:begin status<={1'b0,value[7],(value==0),status[4:0]}; if(operation==OP_PUSH) begin sp<=sp+8'd1;write_memory({8'd0,(sp+8'd1)},value,FINISH);end else write_memory(effective_address,value,FINISH); end CMP_VALUE:begin source_value<=value;operation<=OP_CMP;read_memory(0,DEST_VALUE);end PUSH_HIGH:begin sp<=sp+8'd1;write_memory({8'd0,(sp+8'd1)},return_pc[15:8],PUSH_LOW);end PUSH_LOW:begin sp<=sp+8'd1;write_memory({8'd0,(sp+8'd1)},return_pc[7:0],AFTER_PUSH);end AFTER_PUSH:begin if(operation==OP_CALL && !is_reset && !is_interrupt) begin pc<=effective_address;state<=FINISH;end else read_memory(vector_address,VECTOR_HIGH); end VECTOR_HIGH:begin high_byte<=value;read_memory(vector_address+16'd1,VECTOR_LOW);end VECTOR_LOW:begin pc<={high_byte,value};state<=FINISH;end IRQ_PUSH_PC:state<=PUSH_HIGH; POP_VALUE:begin status<={1'b0,value[7],(value==0),status[4:0]};write_memory(destination_address,value,FINISH); end RETURN_LOW:begin low_byte<=value;read_memory({8'd0,sp},RETURN_HIGH);sp<=sp-8'd1;end RETURN_HIGH:begin pc<={value,low_byte}; if(operation==OP_RETI) begin read_memory({8'd0,sp},RETURN_STATUS);sp<=sp-8'd1;end else state<=FINISH; end RETURN_STATUS:begin status<=value&8'hf0;state<=FINISH;end LDSP_VALUE:begin sp<=value;state<=FINISH;end FINISH:begin if(is_reset) begin initialized<=1;state<=WAIT_STEP;end else begin done<=1;state<=WAIT_RELEASE;end end WAIT_RELEASE:if(!step) begin done<=0;state<=WAIT_STEP;end BUS_WAIT:if(mem_ready) begin value<=mem_read_data;mem_request<=0;state<=BUS_RELEASE;end BUS_RELEASE:if(!mem_ready) state<=return_state; default:state<=RESET_START; endcase end end endmodule