static void
cpu8051_process_interrupt(int pc, int pri)
{
- unsigned char SP;
-
- SP = cpu8051_ReadD(_SP_);
- cpu8051_WriteI(++SP, (cpu8051.pc & 0xFF));
- cpu8051_WriteI(++SP, (cpu8051.pc >> 8));
- cpu8051_WriteD(_SP_, SP);
+ stack_push16(pc);
cpu8051.pc = 0x0B;
cpu8051.active_priority = pri;
}
memory_read8(INT_MEM_ID, _DPTRLOW_);
}
+void
+stack_push8(uint8_t value)
+{
+ uint8_t sp;
+
+ sp = memory_read8(INT_MEM_ID, _SP_);
+
+ memory_write8(INT_MEM_ID, ++sp, value);
+ memory_write8(INT_MEM_ID, _SP_, sp); /* Save new stack pointer */
+}
+
+void
+stack_push16(uint16_t value)
+{
+ uint8_t sp;
+
+ sp = memory_read8(INT_MEM_ID, _SP_);
+
+ memory_write8(INT_MEM_ID, ++sp, (uint8_t) value); /* Write LSB */
+ memory_write8(INT_MEM_ID, ++sp, value >> 8); /* Write MSB */
+ memory_write8(INT_MEM_ID, _SP_, sp); /* Save new stack pointer */
+}
+
+uint8_t
+stack_pop8(void)
+{
+ uint8_t sp;
+ uint8_t value;
+
+ sp = memory_read8(INT_MEM_ID, _SP_);
+
+ value = memory_read8(INT_MEM_ID, sp--);
+ memory_write8(INT_MEM_ID, _SP_, sp); /* Save new stack pointer */
+
+ return value;
+}
+
+uint16_t
+stack_pop16(void)
+{
+ uint8_t sp;
+ uint16_t value;
+
+ sp = memory_read8(INT_MEM_ID, _SP_);
+
+ value = memory_read8(INT_MEM_ID, sp--) << 8; /* Read MSB */
+ value |= memory_read8(INT_MEM_ID, sp--); /* Read LSB */
+ memory_write8(INT_MEM_ID, _SP_, sp); /* Save new stack pointer */
+
+ return value;
+}
+
/* Dump memory */
void
DumpMem(char *Address, char *Asize, int memory_id)
# ACALL
if ($insttype[$i] == 6) {
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "cpu8051_WriteI( ++SP, ( cpu8051.pc & 0x00FF ) );\n";
- print INST_IMP "cpu8051_WriteI( ++SP, ( cpu8051.pc >> 8 ) );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "stack_push16(cpu8051.pc);\n";
}
# LCALL
if ($insttype[$i] == 7) {
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "cpu8051_WriteI( ++SP, ( cpu8051.pc & 0x00FF ) );\n";
- print INST_IMP "cpu8051_WriteI( ++SP, ( cpu8051.pc >> 8 ) );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "stack_push16(cpu8051.pc);\n";
}
# RRC
# RET
if ($insttype[$i] == 11) {
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "cpu8051.pc = ( cpu8051_ReadI( SP-- ) << 8 );\n";
- print INST_IMP "cpu8051.pc += cpu8051_ReadI ( SP-- );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "cpu8051.pc = stack_pop16();\n";
}
# RL
# RETI
if ($insttype[$i] == 15) {
print INST_IMP "cpu8051.active_priority = -1;\n";
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "cpu8051.pc = ( cpu8051_ReadI( SP-- ) << 8 );\n";
- print INST_IMP "cpu8051.pc += cpu8051_ReadI( SP-- );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "cpu8051.pc = stack_pop16();\n";
}
# RLC
# PUSH
if ($insttype[$i] == 35) {
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "cpu8051_WriteI( ++SP, destination );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "stack_push8(destination);\n";
}
# CLR
# POP
if ($insttype[$i] == 39) {
- print INST_IMP "unsigned char SP = cpu8051_ReadD( _SP_ );\n";
- print INST_IMP "destination = cpu8051_ReadI( SP-- );\n";
- print INST_IMP "cpu8051_WriteD( _SP_, SP );\n";
+ print INST_IMP "destination = stack_pop8();\n";
}
# SETB