From: Hugo Villeneuve Date: Sat, 16 Nov 2013 19:22:20 +0000 (-0500) Subject: Add stack push/pop functions X-Git-Tag: v2.0.0~91 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=21fb2cf2a3c16d19ec65a3afacdcb15a90a38340;p=emu8051.git Add stack push/pop functions --- diff --git a/src/cpu8051.c b/src/cpu8051.c index 213f61b..58a0e79 100644 --- a/src/cpu8051.c +++ b/src/cpu8051.c @@ -211,12 +211,7 @@ cpu8051_ReadB(uint8_t bit_address) 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; } diff --git a/src/memory.c b/src/memory.c index 057af56..ac8bca5 100644 --- a/src/memory.c +++ b/src/memory.c @@ -131,6 +131,58 @@ memory_sfr_read_dptr(void) 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) diff --git a/src/memory.h b/src/memory.h index e8ac7ee..c8761e9 100644 --- a/src/memory.h +++ b/src/memory.h @@ -67,6 +67,18 @@ memory_sfr_read8(unsigned long address); u_int16_t memory_sfr_read_dptr(void); +void +stack_push8(uint8_t value); + +void +stack_push16(uint16_t value); + +uint8_t +stack_pop8(void); + +uint16_t +stack_pop16(void); + void DumpMem(char *Address, char *Asize, int memory_id); diff --git a/src/opcode2c.pl b/src/opcode2c.pl index a72c8f7..2c8a149 100755 --- a/src/opcode2c.pl +++ b/src/opcode2c.pl @@ -417,18 +417,12 @@ for ($i=0 ; $i< 256; $i++) { # 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 @@ -450,10 +444,7 @@ for ($i=0 ; $i< 256; $i++) { # 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 @@ -480,10 +471,7 @@ for ($i=0 ; $i< 256; $i++) { # 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 @@ -605,9 +593,7 @@ for ($i=0 ; $i< 256; $i++) { # 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 @@ -629,9 +615,7 @@ for ($i=0 ; $i< 256; $i++) { # 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