Introduce pseudo-halt

This commit is contained in:
2024-10-23 16:29:11 -05:00
parent 8f9950f39e
commit 226da7a5b3
4 changed files with 368 additions and 95 deletions
+133 -95
View File
@@ -10,97 +10,86 @@
#define CPU_HALTING 1
#define CPU_FATAL 2
#define CPU_FETCH 3
#define CPU_ILLEGAL 4
#define CPU_BITSTRING 5
#define CPU_FLOATENDO 6
#define CPU_ADD_IMM 7
#define CPU_ADD_REG 8
#define CPU_ADDF_S 9
#define CPU_ADDI 10
#define CPU_AND 11
#define CPU_ANDI 12
#define CPU_BCOND 13
#define CPU_CAXI 14
#define CPU_CLI 15
#define CPU_CMP_IMM 16
#define CPU_CMP_REG 17
#define CPU_CMPF_S 18
#define CPU_CVT_SW 19
#define CPU_CVT_WS 20
#define CPU_DIV 21
#define CPU_DIVF_S 22
#define CPU_DIVU 23
#define CPU_HALT 24
#define CPU_IN_B 25
#define CPU_IN_H 26
#define CPU_IN_W 27
#define CPU_JAL 28
#define CPU_JMP 29
#define CPU_JR 30
#define CPU_LD_B 31
#define CPU_LD_H 32
#define CPU_LD_W 33
#define CPU_LDSR 34
#define CPU_MOV_IMM 35
#define CPU_MOV_REG 36
#define CPU_MOVEA 37
#define CPU_MOVHI 38
#define CPU_MPYHW 39
#define CPU_MUL 40
#define CPU_MULF_S 41
#define CPU_MULU 42
#define CPU_NOT 43
#define CPU_OR 44
#define CPU_ORI 45
#define CPU_OUT_B 46
#define CPU_OUT_H 47
#define CPU_OUT_W 48
#define CPU_RETI 49
#define CPU_REV 50
#define CPU_SAR_IMM 51
#define CPU_SAR_REG 52
#define CPU_SCH0BSD 53
#define CPU_SCH0BSU 54
#define CPU_SCH1BSD 55
#define CPU_SCH1BSU 56
#define CPU_SEI 57
#define CPU_SETF 58
#define CPU_SHL_IMM 59
#define CPU_SHL_REG 60
#define CPU_SHR_IMM 61
#define CPU_SHR_REG 62
#define CPU_ST_B 63
#define CPU_ST_H 64
#define CPU_ST_W 65
#define CPU_STSR 66
#define CPU_SUB 67
#define CPU_SUBF_S 68
#define CPU_TRAP 69
#define CPU_TRNC_SW 70
#define CPU_XB 71
#define CPU_XH 72
#define CPU_XOR 73
#define CPU_XORI 74
#define CPU_ANDBSU 75 /* Keep bit string ALU commands sequential */
#define CPU_ANDNBSU 76
#define CPU_MOVBSU 77
#define CPU_NOTBSU 78
#define CPU_ORBSU 79
#define CPU_ORNBSU 80
#define CPU_XORBSU 81
#define CPU_XORNBSU 82
/* Abstract operand types */
#define CPU_IMP(x) -x-1
#define CPU_DISP9 1
#define CPU_DISP26 2
#define CPU_IMM16S 3
#define CPU_IMM16U 4
#define CPU_IMM5S 5
#define CPU_IMM5U 6
#define CPU_MEM 7
#define CPU_REG1 8
#define CPU_REG2 9
#define CPU_PHALT 4
#define CPU_ILLEGAL 5
#define CPU_BITSTRING 6
#define CPU_FLOATENDO 7
#define CPU_ADD_IMM 8
#define CPU_ADD_REG 9
#define CPU_ADDF_S 10
#define CPU_ADDI 11
#define CPU_AND 12
#define CPU_ANDI 13
#define CPU_BCOND 14
#define CPU_CAXI 15
#define CPU_CLI 16
#define CPU_CMP_IMM 17
#define CPU_CMP_REG 18
#define CPU_CMPF_S 19
#define CPU_CVT_SW 20
#define CPU_CVT_WS 21
#define CPU_DIV 22
#define CPU_DIVF_S 23
#define CPU_DIVU 24
#define CPU_HALT 25
#define CPU_IN_B 26
#define CPU_IN_H 27
#define CPU_IN_W 28
#define CPU_JAL 29
#define CPU_JMP 30
#define CPU_JR 31
#define CPU_LD_B 32
#define CPU_LD_H 33
#define CPU_LD_W 34
#define CPU_LDSR 35
#define CPU_MOV_IMM 36
#define CPU_MOV_REG 37
#define CPU_MOVEA 38
#define CPU_MOVHI 39
#define CPU_MPYHW 40
#define CPU_MUL 41
#define CPU_MULF_S 42
#define CPU_MULU 43
#define CPU_NOT 44
#define CPU_OR 45
#define CPU_ORI 46
#define CPU_OUT_B 47
#define CPU_OUT_H 48
#define CPU_OUT_W 49
#define CPU_RETI 50
#define CPU_REV 51
#define CPU_SAR_IMM 52
#define CPU_SAR_REG 53
#define CPU_SCH0BSD 54
#define CPU_SCH0BSU 55
#define CPU_SCH1BSD 56
#define CPU_SCH1BSU 57
#define CPU_SEI 58
#define CPU_SETF 59
#define CPU_SHL_IMM 60
#define CPU_SHL_REG 61
#define CPU_SHR_IMM 62
#define CPU_SHR_REG 63
#define CPU_ST_B 64
#define CPU_ST_H 65
#define CPU_ST_W 66
#define CPU_STSR 67
#define CPU_SUB 68
#define CPU_SUBF_S 69
#define CPU_TRAP 70
#define CPU_TRNC_SW 71
#define CPU_XB 72
#define CPU_XH 73
#define CPU_XOR 74
#define CPU_XORI 75
#define CPU_ANDBSU 76 /* Keep bit string ALU IDs consecutive */
#define CPU_ANDNBSU 77
#define CPU_MOVBSU 78
#define CPU_NOTBSU 79
#define CPU_ORBSU 80
#define CPU_ORNBSU 81
#define CPU_XORBSU 82
#define CPU_XORNBSU 83
/* Functional operand types */
#define CPU_LITERAL 0
@@ -206,6 +195,13 @@ static const uint8_t OPDEFS_FLOATENDO[] = {
/**************************** Forward references *****************************/
static int phAssess (VB *, uint32_t, int, int32_t);
static uint32_t phUntil (VB *);
/***************************** Callback Handlers *****************************/
/* Prepare to handle an exception */
@@ -248,6 +244,10 @@ static int cpuRead(VB *sim, uint32_t address, int type, int32_t *value) {
/* Retrieve the value from the simulation state directly */
busRead(sim, address, type, value);
/* Check for pseudo-halt */
if (sim->ph.enabled && phAssess(sim, address, type, *value))
return 0;
/* Invoke the callback if available */
if (
sim->onRead != NULL &&
@@ -296,6 +296,10 @@ static int cpuWrite(VB *sim, uint32_t address, int type, int32_t value) {
int cancel = 0;
uint32_t cycles = 3; /* TODO: Research this */
/* Reset pseudo-halt */
if (sim->ph.enabled)
sim->ph.step = 0;
/* Invoke the callback if available */
if (
sim->onWrite != NULL &&
@@ -482,10 +486,13 @@ static int cpuException(VB *sim) {
/* Interrupts only */
if ((cause & 0xFF00) == 0xFE00) {
sim->cpu.psw.i = (cause >> 4 & 7) + 1;
/* HALT instruction */
if (sim->cpu.halt) {
sim->cpu.halt = 0;
sim->cpu.pc += 2;
}
}
/* All exceptions */
@@ -583,6 +590,30 @@ static int cpuHalt(VB *sim) {
return !cpuIRQ(sim);
}
/* Pseudo-halt is pending */
static int cpuPHalt(VB *sim) {
int32_t value; /* Value read from memory */
/* An interrupt will be accepted */
if (cpuIRQ(sim)) {
sim->ph.step = 0;
return 0;
}
/* Monitor value has not changed */
busRead(sim, sim->ph.address, sim->ph.type, &value);
if (value == sim->ph.value) {
sim->cpu.clocks = phUntil(sim);
return 1;
}
/* Release pseudo-halt */
sim->cpu.operation = CPU_FETCH;
sim->cpu.step = 0;
sim->ph.step = 0;
return 0;
}
/**************************** Instruction Helpers ****************************/
@@ -1699,10 +1730,11 @@ static int cpuEmulate(VB *sim, uint32_t clocks) {
/* Processing by operation ID */
switch (sim->cpu.operation) {
case CPU_EXCEPTION: brk = cpuException(sim); break;
case CPU_EXCEPTION: brk = cpuException(sim); break;
case CPU_FATAL : return 0;
case CPU_FETCH : brk = cpuFetch (sim); break;
case CPU_HALTING : if (cpuHalt(sim)) return 0; break;
case CPU_FETCH : brk = cpuFetch (sim); break;
case CPU_HALTING : if (cpuHalt (sim)) return 0; break;
case CPU_PHALT : if (cpuPHalt(sim)) return 0; break;
case CPU_ADD_IMM: cpuADDImm (sim); break;
case CPU_ADD_REG: cpuADDReg (sim); break;
@@ -1826,8 +1858,14 @@ static void cpuReset(VB *sim) {
/* Determine how many clocks are guaranteed to process */
static uint32_t cpuUntil(VB *sim, uint32_t clocks) {
/* Pseudo-halting */
if (sim->cpu.operation == CPU_PHALT) {
if (!sim->ph.operation)
return clocks;
}
/* Halting */
if (sim->cpu.halt) {
else if (sim->cpu.halt) {
return
sim->cpu.operation == CPU_HALTING &&
!(sim->cpu.psw.id | sim->cpu.psw.ep | sim->cpu.psw.np) &&