Add overflow and auxiliary carry flags manipulation functions
[emu8051.git] / src / opcode2c.pl
index cf9aa60..da2f235 100755 (executable)
@@ -51,6 +51,7 @@ print INST_IMP "#define INSTRUCTIONS_8051_M 1\n\n\n";
 print INST_IMP "#include \"reg8051.h\"\n";
 print INST_IMP "#include \"cpu8051.h\"\n";
 print INST_IMP "#include \"memory.h\"\n";
+print INST_IMP "#include \"psw.h\"\n";
 print INST_IMP "#include \"instructions_8051.h\"\n\n\n";
 
 # Header for disasm.h
@@ -78,7 +79,7 @@ print DISASM_H " * along with this program; if not, write to the Free Software\n
 print DISASM_H " * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.\n";
 print DISASM_H "*/\n\n";
 print DISASM_H "#ifndef DISASM_H\n";
-print DISASM_H "#define DISASM_H 1\n\n\n";
+print DISASM_H "#define DISASM_H 1\n\n";
 
 $nbinst=0;
 $nbaddr=0;
@@ -116,7 +117,7 @@ while($ligne=<OPCODELST>) {
            $instargs[($instnumb << 2) + $i + 1]=$argstypes{$argslist[$i]};
        }
     }
-       
+
     if (not exists $insttext{$wordlist[2]}) {
        $insttext[$nbinst]=$wordlist[2];
        $insttext{$wordlist[2]}=$nbinst++;
@@ -136,9 +137,12 @@ while($ligne=<OPCODELST>) {
     $instnumb++;
 }
 # ------------------------------------------------------------------------------
-print DISASM_H "// For all 256 opcodes, the value in this table gives the instruction type\n";
-print DISASM_H "// ex.: MOV, INC, CLR, CPL,...\n";
-print DISASM_H "// To know what is the instruction type, use the number as an offset in the InstTextTbl[]\n";
+print DISASM_H "/*\n";
+print DISASM_H " * For all 256 opcodes, the value in this table gives the instruction type\n";
+print DISASM_H " * ex.: MOV, INC, CLR, CPL,...\n";
+print DISASM_H " * To know what is the instruction type, use\n";
+print DISASM_H " * the number as an offset in the InstTextTbl[]\n";
+print DISASM_H " */\n";
 print DISASM_H "static int InstTypesTbl[] = {\n";
 for($i=0;$i<256;$i++) {
     print DISASM_H " $insttype[$i]";
@@ -146,9 +150,9 @@ for($i=0;$i<256;$i++) {
     if (($i+1) % 16 == 0) { print DISASM_H "\n"; }
 }
 print DISASM_H "};\n";
-print DISASM_H "\n\n";
+print DISASM_H "\n";
 # ------------------------------------------------------------------------------
-print DISASM_H "// Size(in bytes) of each instruction (offset in table is instruction opcode)\n";
+print DISASM_H "/* Size(in bytes) of each instruction (offset in table is instruction opcode) */\n";
 print DISASM_H "static int InstSizesTbl[] = {\n";
 for($i=0;$i<256;$i++) {
     print DISASM_H " $nbbytes[$i]";
@@ -156,9 +160,9 @@ for($i=0;$i<256;$i++) {
     if (($i+1) % 16 == 0) { print DISASM_H "\n"; }
 }
 print DISASM_H "};\n";
-print DISASM_H "\n\n";
+print DISASM_H "\n";
 # ------------------------------------------------------------------------------
-print DISASM_H "// List of instructions types referenced by InstTypesTbl[]\n";
+print DISASM_H "/* List of instructions types referenced by InstTypesTbl[] */\n";
 $nbelement=@insttext;
 print DISASM_H "\#define InstTextTblLength $nbelement\n";
 $elementnb=0;
@@ -169,14 +173,16 @@ foreach $instruc (@insttext) {
     print DISASM_H "\n";
 }
 print DISASM_H "};\n";
-print DISASM_H "\n\n";
+print DISASM_H "\n";
 # ------------------------------------------------------------------------------
-print DISASM_H "// Table describing all arguments types of an instruction\n";
-print DISASM_H "// The table is indexed InstArgTbl[ opcode * 4]\n";
-print DISASM_H "// InstArgTbl[opcode*4 + 1] gives the number of arguments the instruction has\n";
-print DISASM_H "// InstArgTbl[opcode*4 + i] for i=1,2 and 3 give the type of each argument\n";
-print DISASM_H "// for most instructions, the 3rd argument isn't used\n";
-print DISASM_H "// the argument type is referecing to ArgsTextTbl[]\n";
+print DISASM_H "/*\n";
+print DISASM_H " * Table describing all arguments types of an instruction\n";
+print DISASM_H " * The table is indexed InstArgTbl[ opcode * 4]\n";
+print DISASM_H " * InstArgTbl[opcode*4 + 1] gives the number of arguments the instruction has\n";
+print DISASM_H " * InstArgTbl[opcode*4 + i] for i=1,2 and 3 give the type of each argument\n";
+print DISASM_H " * for most instructions, the 3rd argument isn't used\n";
+print DISASM_H " * the argument type is referecing to ArgsTextTbl[]\n";
+print DISASM_H " */\n";
 print DISASM_H "\#define InstArgTblLength 256\n";
 print DISASM_H "static int InstArgTbl[] = {\n";
 for($i=0;$i<1024;$i++) {
@@ -185,10 +191,12 @@ for($i=0;$i<1024;$i++) {
     if (($i+1) % 16 == 0) { print DISASM_H "\n"; }
 }
 print DISASM_H "};\n";
-print DISASM_H "\n\n";
+print DISASM_H "\n";
 # ------------------------------------------------------------------------------
-print DISASM_H "// List all types of arguments available to instructions\n";
-print DISASM_H "// Referenced by InstArgsTbl[]\n";
+print DISASM_H "/*\n";
+print DISASM_H " * List all types of arguments available to instructions\n";
+print DISASM_H " * Referenced by InstArgsTbl[]\n";
+print DISASM_H " */\n";
 $nbelement=@argstypes;
 print DISASM_H "\#define ArgsTextTblLength $nbelement\n";
 $elementnb=0;
@@ -199,7 +207,7 @@ foreach $args (@argstypes) {
     print DISASM_H "\n";
 }
 print DISASM_H "};\n";
-print DISASM_H "\n\n";
+print DISASM_H "\n";
 
 # ------------------------------------------------------------------------------
 for ($i=0 ; $i< 256; $i++) {
@@ -207,9 +215,7 @@ for ($i=0 ; $i< 256; $i++) {
     print INST_IMP " * Instruction \"$a_instruction[$i]\" takes $a_cycles[$i] cycle(s) and $a_bytes[$i] byte(s).\n";
     print INST_IMP " ","*"x76,"/\n";
     print INST_IMP "int\n";
-    print INST_IMP "cpu8051_OP_$a_opcodehex[$i]( void )\n";
-# TEST hugo new...
-#    print INST_DEF "int OP_$a_opcodehex[$i]( );\n";
+    print INST_IMP "cpu8051_OP_$a_opcodehex[$i](void)\n";
     print INST_IMP "{\n";
 
     if( $i == 0x85 ) {
@@ -225,7 +231,8 @@ for ($i=0 ; $i< 256; $i++) {
        if ($instargs[$i*4] > 0) {
            $op_destination=$instargs[$i*4+1];
            if ($op_destination == 0) { # addr11
-               print INST_IMP "  unsigned int addr11 = ( ( memory_read8( PGM_MEM_ID, cpu8051.pc - 1 ) << 3 ) & 0xF00 ) + memory_read8( PGM_MEM_ID, cpu8051.pc++ );\n";
+               print INST_IMP "  unsigned int addr11 = ( ( memory_read8( PGM_MEM_ID, cpu8051.pc - 1 ) << 3 ) & 0xF00 ) + memory_read8( PGM_MEM_ID, cpu8051.pc );\n";
+               print INST_IMP "  cpu8051.pc++;\n";
            }
            if ($op_destination == 1) { # addr16
                print INST_IMP "unsigned int addr16 = ( memory_read8( PGM_MEM_ID, cpu8051.pc++ ) << 8 );\n";
@@ -244,7 +251,7 @@ for ($i=0 ; $i< 256; $i++) {
            if ($op_destination == 5) { # @R1
                print INST_IMP "unsigned char destination = cpu8051_ReadI ( cpu8051_ReadD( BANKPSW + _R1_ ) );\n";
            }
-           
+
            if ($op_destination == 6) { # R0
                print INST_IMP "unsigned char destination = cpu8051_ReadD( BANKPSW + _R0_ );\n";
            }
@@ -275,16 +282,16 @@ for ($i=0 ; $i< 256; $i++) {
            }
            if ($op_destination == 15) { # reladdr
                print INST_IMP "cpu8051.pc++;\n";
-               print INST_IMP "unsigned int destination = ( ( memory_read8( PGM_MEM_ID, cpu8051.pc - 1 ) + cpu8051.pc ) & 0xFF ) + ( cpu8051.pc & 0xFF00 );\n";
+               print INST_IMP "unsigned int destination = ((char) memory_read8(PGM_MEM_ID, cpu8051.pc - 1)) + cpu8051.pc;\n";
            }
            if ($op_destination == 16) { # #data
                print INST_IMP "unsigned char destination = memory_read8( PGM_MEM_ID, cpu8051.pc++ );\n";
            }
            if ($op_destination == 17) { # C
-               print INST_IMP "unsigned char destination = ( cpu8051_ReadD( _PSW_ ) >> 7 );\n";
+               print INST_IMP "unsigned char destination = psw_read_cy();\n";
            }
            if ($op_destination == 18) { # @A+DPTR
-               print INST_IMP "unsigned int destination = cpu8051_ReadI( cpu8051_ReadD( _ACC_ ) + cpu8051_ReadD( _DPTRLOW_ ) + ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) );\n";
+               print INST_IMP "unsigned int destination = cpu8051_ReadD( _ACC_ ) + memory_sfr_read_dptr();\n";
            }
 # Mis en commentaire car donnait un warning (destination et source unused variables...)
 #      if ($op_destination == 20) { # AB
@@ -292,7 +299,7 @@ for ($i=0 ; $i< 256; $i++) {
 #          print INST_IMP "unsigned char source = cpu8051_ReadD( _B_ );\n";
 #      }
            if ($op_destination == 21) { # DPTR
-               print INST_IMP "unsigned int destination = ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_ );\n";
+               print INST_IMP "unsigned int destination = memory_sfr_read_dptr();\n";
            }
            if ($op_destination == 22) { # #data16
                print INST_IMP "unsigned char destination = ( memory_read8( PGM_MEM_ID, (cpu8051.pc)++ ) << 8 );\n";
@@ -303,7 +310,7 @@ for ($i=0 ; $i< 256; $i++) {
                print INST_IMP "destination = ( cpu8051_ReadB( dstbitaddr ) ^ 1 );\n";
            }
            if ($op_destination == 24) { # @DPTR
-               print INST_IMP "unsigned char destination = cpu8051_ReadI( ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_) );\n";
+               print INST_IMP "unsigned char destination = cpu8051_ReadI(memory_sfr_read_dptr());\n";
            }
        }
 
@@ -360,22 +367,22 @@ for ($i=0 ; $i< 256; $i++) {
            }
            if ($op_source == 15) { # reladdr
                print INST_IMP "(cpu8051.pc)++;\n";
-               print INST_IMP "unsigned int source = ( ( memory_read8( PGM_MEM_ID, cpu8051.pc - 1 ) + cpu8051.pc ) & 0xFF ) + ( cpu8051.pc & 0xFF00 );\n";
+               print INST_IMP "unsigned int source = ((char) memory_read8(PGM_MEM_ID, cpu8051.pc - 1)) + cpu8051.pc;\n";
            }
            if ($op_source == 16) { # #data
                print INST_IMP "unsigned char source = memory_read8( PGM_MEM_ID, (cpu8051.pc)++ );\n";
            }
            if ($op_source == 17) { # C
-               print INST_IMP "unsigned char source = ( cpu8051_ReadD( _PSW_ ) >> 7 );\n";
+               print INST_IMP "unsigned char source = psw_read_cy();\n";
            }
            if ($op_source == 18) { # @A+DPTR
-               print INST_IMP "unsigned char source = memory_read8( PGM_MEM_ID, cpu8051_ReadD( _ACC_ ) + cpu8051_ReadD( _DPTRLOW_ ) + ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) );\n";
+               print INST_IMP "unsigned char source = memory_read8( PGM_MEM_ID, cpu8051_ReadD( _ACC_ ) + memory_sfr_read_dptr());\n";
            }
            if ($op_source == 19) { # @A+PC
                print INST_IMP "unsigned char source = memory_read8( PGM_MEM_ID, cpu8051_ReadD( _ACC_ ) + ( ++cpu8051.pc ) );\n";
            }
            if ($op_source == 21) { # DPTR
-               print INST_IMP "unsigned int source = ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_ );\n";
+               print INST_IMP "unsigned int source = memory_sfr_read_dptr();\n";
            }
            if ($op_source == 22) { # #data16
                print INST_IMP "unsigned char source = ( memory_read8( PGM_MEM_ID, (cpu8051.pc)++ ) << 8 );\n";
@@ -386,7 +393,7 @@ for ($i=0 ; $i< 256; $i++) {
                print INST_IMP "source = ( cpu8051_ReadB( srcbitaddr ) ^ 1 );\n";
            }
            if ($op_source == 24) { # @DPTR
-               print INST_IMP "unsigned char source = cpu8051_ReadI( ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_) );\n";
+               print INST_IMP "unsigned char source = cpu8051_ReadI(memory_sfr_read_dptr());\n";
            }
        }
 
@@ -411,25 +418,19 @@ 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
        if ($insttype[$i] == 8) {
-           print INST_IMP "unsigned char tmpval = destination;\n";
-           print INST_IMP "destination = ( destination >> 1 ) | ( cpu8051_ReadD( _PSW_ ) & 0x80 );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x7F ) | ( tmpval << 7 ) );\n";
+           print INST_IMP "unsigned char new_cy = destination & 0x01;\n";
+           print INST_IMP "destination = ( destination >> 1 ) | (psw_read_cy() << 7);\n";
+           print INST_IMP "psw_write_cy(new_cy);\n";
        }
 
        # DEC
@@ -444,10 +445,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
@@ -457,12 +455,24 @@ for ($i=0 ; $i< 256; $i++) {
 
        # ADD
        if ($insttype[$i] == 13) {
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x3B ) );\n";
+           print INST_IMP "psw_clr_cy();\n";
+           print INST_IMP "psw_clr_ac();\n";
+           print INST_IMP "psw_clr_ov();\n";
+            # The Overflow (OV) bit is set if there is a carry-out of bit 6 or
+            # out of bit 7, but not both. In other words, if the addition of the
+            # Accumulator, operand and (in the case of ADDC) the Carry flag
+            # treated as signed values results in a value that is out of the
+            # range of a signed byte (-128 through +127) the Overflow flag is
+            # set. Otherwise, the Overflow flag is cleared.
            print INST_IMP "if ( destination + source > 0xFF ) {\n";
-           print INST_IMP "   cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
-           print INST_IMP "   if ( ( destination & 0x7F ) + ( source & 0x7F ) < 0x80 )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "} else if ( ( destination & 0x7F ) + ( source & 0x7F ) > 0x7F )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "if ( ( destination & 0x0F ) + ( source & 0x0F ) > 0x0F )   cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
+            # Carry from bit 7
+           print INST_IMP "   psw_set_cy();\n";
+            # If no carry from bit 6, set OV
+           print INST_IMP "   if (((destination & 0x7F) + (source & 0x7F)) < 0x80)\n";
+           print INST_IMP "       psw_set_ov();\n";
+            # If no carry from bit 7, but caary from bit 6, set OV
+           print INST_IMP "} else if (((destination & 0x7F) + (source & 0x7F)) > 0x7F )  psw_set_ov();\n";
+           print INST_IMP "if (((destination & 0x0F) + (source & 0x0F)) > 0x0F)  psw_set_ac();\n";
            print INST_IMP "destination += source;\n";
        }
 
@@ -474,60 +484,49 @@ 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.pc = stack_pop16();\n";
        }
 
        # RLC
        if ($insttype[$i] == 16) {
-           print INST_IMP "unsigned char tmpval = destination;\n";
-           print INST_IMP "destination = ( destination << 1 ) | ( ( cpu8051_ReadD( _PSW_ ) & 0x80 ) >> 7 );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x7F ) | ( tmpval & 0x80 ) );\n";
+           print INST_IMP "unsigned char new_cy = destination & 0x80;\n";
+           print INST_IMP "destination = ( destination << 1 ) | psw_read_cy();\n";
+           print INST_IMP "psw_write_cy(new_cy);\n";
        }
 
        # ADDC
        if ($insttype[$i] == 17) {
-           print INST_IMP "unsigned char carryflag = ( cpu8051_ReadD( _PSW_ ) >> 7 );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x3B ) );\n";
+           print INST_IMP "unsigned char carryflag = psw_read_cy();\n";
+           print INST_IMP "psw_clr_cy();\n";
+           print INST_IMP "psw_clr_ac();\n";
+           print INST_IMP "psw_clr_ov();\n";
            print INST_IMP "if ( destination + source + carryflag > 0xFF ) {\n";
-           print INST_IMP "   cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
-           print INST_IMP "   if ( ( destination & 0x7F ) + ( source & 0x7F ) + carryflag < 0x80 )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "} else if ( ( destination & 0x7F ) + ( source & 0x7F ) + carryflag > 0x7F )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "if ( ( destination & 0x0F ) + ( source & 0x0F ) + carryflag > 0x0F )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x40 ) );\n";
+           print INST_IMP "   psw_set_cy();\n";
+           print INST_IMP "   if (((destination & 0x7F) + (source & 0x7F) + carryflag) < 0x80);\n";
+           print INST_IMP "       psw_set_ov();\n";
+           print INST_IMP "} else if (((destination & 0x7F) + (source & 0x7F) + carryflag) > 0x7F)  psw_set_ov();\n";
+           print INST_IMP "if (((destination & 0x0F) + (source & 0x0F) + carryflag) > 0x0F)  psw_set_ac();\n";
            print INST_IMP "destination += source;\n";
        }
 
        # JC
        if ($insttype[$i] == 18) {
-           print INST_IMP "if ( cpu8051_ReadD( _PSW_ ) > 0x7F) { cpu8051.pc = destination; }\n";
+           print INST_IMP "if (psw_read_cy()) { cpu8051.pc = destination; }\n";
        }
 
        # ORL
        if ($insttype[$i] == 19) {
-           if ($instargs[$i*4+1] == 17) {
-               # sur des bits
-               print INST_IMP "cpu8051_WriteD( _PSW_ , ( ( destination | source ) << 7 ) );\n";
-           } else {
-               # sur des bytes
-               print INST_IMP "destination |= source;\n";
-           }
+            print INST_IMP "destination |= source;\n";
        }
 
        # JNC
        if ($insttype[$i] == 20) {
-           print INST_IMP "if ( cpu8051_ReadD( _PSW_ ) < 0x80 ) { cpu8051.pc = destination; }\n";
+           print INST_IMP "if (psw_read_cy() == 0) { cpu8051.pc = destination; }\n";
        }
 
        # ANL
        if ($insttype[$i] == 21) {
-           if ($instargs[$i*4+1] == 17) {
-               # sur des bits
-               print INST_IMP "cpu8051_WriteD( _PSW_, ( ( destination & source) << 7 ) );\n";
-           } else {
-               # sur des bytes
-               print INST_IMP "destination &= source;\n";
-           }
+            print INST_IMP "destination &= source;\n";
        }
 
        # JZ
@@ -568,30 +567,41 @@ for ($i=0 ; $i< 256; $i++) {
        # DIV
        if ($insttype[$i] == 29) {
            print INST_IMP "unsigned char A = cpu8051_ReadD( _ACC_ ), B = cpu8051_ReadD( _B_ );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x7B ) );\n";
+           print INST_IMP "psw_clr_cy();\n";
+            # If B is zero, the OV flag will be set indicating a
+            # division-by-zero error
            print INST_IMP "if ( B != 0 ) {\n";
-           print INST_IMP "cpu8051_WriteD( _ACC_, A/B ); cpu8051_WriteD( _B_, A%B );\n";
-           print INST_IMP "} else cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
+           print INST_IMP "    cpu8051_WriteD( _ACC_, A/B ); cpu8051_WriteD( _B_, A%B );\n";
+           print INST_IMP "    psw_clr_ov();\n";
+           print INST_IMP "} else {\n";
+           print INST_IMP "    psw_set_ov();\n";
+           print INST_IMP "}\n";
        }
 
        # SUBB
        if ($insttype[$i] == 30) {
-           print INST_IMP "unsigned char carryflag = cpu8051_ReadD( _PSW_ ) >> 7;\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x3B ) );\n";
+           print INST_IMP "unsigned char carryflag = psw_read_cy();\n";
+           print INST_IMP "psw_clr_cy();\n";
+           print INST_IMP "psw_clr_ac();\n";
+           print INST_IMP "psw_clr_ov();\n";
            print INST_IMP "if ( destination < ( source + carryflag ) ) {\n";
-           print INST_IMP "  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
-           print INST_IMP "  if ( ( destination & 0x7F ) > ( ( source + carryflag ) & 0x7F ) )  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "} else if ( ( destination & 0x7F ) < ( ( source + carryflag ) & 0x7F ) )   cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
-           print INST_IMP "if ( ( destination & 0x0F ) < ( ( source + carryflag ) & 0x0F ) )   cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x40 ) );\n";
+           print INST_IMP "  psw_set_cy();\n";
+           print INST_IMP "  if ((destination & 0x7F) > ((source + carryflag) & 0x7F))  psw_set_ov();\n";
+           print INST_IMP "} else if ((destination & 0x7F) < ((source + carryflag) & 0x7F))   psw_set_ov();\n";
+           print INST_IMP "if ((destination & 0x0F) < ((source + carryflag) & 0x0F))   psw_set_ac();\n";
            print INST_IMP "destination -= source + carryflag;\n";
        }
 
        # MUL
        if ($insttype[$i] == 31) {
            print INST_IMP "unsigned char A = cpu8051_ReadD( _ACC_ ), B = cpu8051_ReadD( _B_ );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x7B ) );\n";
+           print INST_IMP "psw_clr_cy();\n";
+           print INST_IMP "psw_clr_ov();\n";
            print INST_IMP "cpu8051_WriteD( _ACC_ , ( ( A * B ) & 0x00FF ) ); cpu8051_WriteD( _B_, ( A * B ) / 0x100 );\n";
-           print INST_IMP "if ( cpu8051_ReadD( _B_ ) > 0) cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x04 ) );\n";
+           print INST_IMP "if (cpu8051_ReadD(_B_) > 0)\n";
+           print INST_IMP "    psw_set_ov();\n";
+           print INST_IMP "else\n";
+           print INST_IMP "    psw_clr_ov();\n";
        }
 
        # CPL
@@ -602,17 +612,15 @@ for ($i=0 ; $i< 256; $i++) {
 
        # CJNE
        if ($insttype[$i] == 34) {
-           print INST_IMP "unsigned int reladdr = ( ( memory_read8( PGM_MEM_ID, cpu8051.pc ) + ( ( cpu8051.pc + 1 ) & 0x00FF ) ) & 0x00FF ) + ( ( cpu8051.pc + 1 ) & 0xFF00 );\n";
-           print INST_IMP "cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) & 0x7F ) );\n";
-           print INST_IMP "if ( destination < source ) cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
-           print INST_IMP "if ( destination != source ) cpu8051.pc = reladdr;\n";
+           print INST_IMP "unsigned int reladdr = ((char) memory_read8(PGM_MEM_ID, cpu8051.pc)) + (cpu8051.pc + 1);\n";
+           print INST_IMP "psw_clr_cy();\n";
+           print INST_IMP "if ( destination < source ) psw_set_cy();\n";
+           print INST_IMP "if ( destination != source ) cpu8051.pc = reladdr; else cpu8051.pc++; \n";
        }
 
        # 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
@@ -634,9 +642,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
@@ -646,12 +652,12 @@ for ($i=0 ; $i< 256; $i++) {
 
        # DA
        if ($insttype[$i] == 41) {
-           print INST_IMP "if ( ( ( destination & 0x0F ) > 9) || ( cpu8051_ReadD( _PSW_ ) | 0x40)) {\n";
-           print INST_IMP "   if ( ( destination + 6 ) > 0xFF)  cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
+           print INST_IMP "if (((destination & 0x0F) > 9) || psw_read_ac()) {\n";
+           print INST_IMP "   if ( ( destination + 6 ) > 0xFF)  psw_set_cy();\n";
            print INST_IMP "   destination += 6;\n";
            print INST_IMP "}\n";
-           print INST_IMP "if ( ( cpu8051_ReadD( _PSW_ ) & 0x80) || ( ( destination & 0xF0 ) > 0x90 ) ) {\n";
-           print INST_IMP "   if ( ( destination + 0x60 ) > 0xFF ) cpu8051_WriteD( _PSW_, ( cpu8051_ReadD( _PSW_ ) | 0x80 ) );\n";
+           print INST_IMP "if ( psw_read_cy() || ( ( destination & 0xF0 ) > 0x90 ) ) {\n";
+           print INST_IMP "   if ( ( destination + 0x60 ) > 0xFF ) psw_set_cy();\n";
            print INST_IMP "   destination += 0x60;\n";
            print INST_IMP "}\n";
        }
@@ -729,17 +735,16 @@ for ($i=0 ; $i< 256; $i++) {
                print INST_IMP "cpu8051_WriteB( dstbitaddr, destination );\n";
            }
            if ($op_destination == 17) { # C
-               print INST_IMP "cpu8051_WriteD( _PSW_, ( ( cpu8051_ReadD( _PSW_ ) & 0x7F) | ( destination << 7 ) ) );\n";
+               print INST_IMP "psw_write_cy(destination);\n";
            }
            if ($op_destination == 21) { # DPTR
-               print INST_IMP "cpu8051_WriteD( _DPTRHIGH_, ( destination >> 8 ) );\n";
-               print INST_IMP "cpu8051_WriteD( _DPTRLOW_, ( destination & 0xFF ) );\n";
+               print INST_IMP "memory_sfr_write_dptr(destination);\n";
            }
            if ($op_destination == 23) { # /bitaddr
                print INST_IMP "cpu8051_WriteB( dstbitaddr, destination );\n";
            }
            if ($op_destination == 24) { # @DPTR
-               print INST_IMP "cpu8051_WriteI( ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_ ), destination );\n";
+               print INST_IMP "cpu8051_WriteI(memory_sfr_read_dptr(), destination);\n";
            }
        }
 
@@ -792,17 +797,16 @@ for ($i=0 ; $i< 256; $i++) {
                    print INST_IMP "cpu8051_WriteB( srcbitaddr, source );\n";
                }
                if ($op_source == 17) { # C
-                   print INST_IMP "cpu8051_WriteD( _PSW_, ( ( cpu8051_ReadD( _PSW_ ) & 0x7F) | ( source << 7 ) ) );\n";
+                   print INST_IMP "psw_write_cy(source);\n";
                }
                if ($op_source == 21) { # DPTR
-                   print INST_IMP "cpu8051_WriteD( _DPTRHIGH_, ( source >> 8 ) );\n";
-                   print INST_IMP "cpu8051_WriteD( _DPTRLOW_, ( source & 0xFF ) );\n";
+                    print INST_IMP "memory_sfr_write_dptr(source);\n";
                }
                if ($op_source == 23) { # /bitaddr
                    print INST_IMP "cpu8051_WriteB( srcbitaddr, source );\n";
                }
                if ($op_source == 24) { # @DPTR
-                   print INST_IMP "cpu8051_WriteI( ( cpu8051_ReadD( _DPTRHIGH_ ) << 8 ) + cpu8051_ReadD( _DPTRLOW_ ), source );\n";
+                   print INST_IMP "cpu8051_WriteI(memory_sfr_read_dptr(), source);\n";
                }
            }
        }
@@ -841,11 +845,11 @@ print INST_DEF "*/\n\n";
 
 print INST_DEF "#ifndef INSTRUCTIONS_8051_H\n";
 print INST_DEF "#define INSTRUCTIONS_8051_H 1\n\n\n";
-print INST_DEF "#define BANKPSW ( cpu8051_ReadD( _PSW_ ) & 0x18 )\n\n";
-print INST_DEF "typedef int (*OPCODE_FP)( void );\n\n\n";
+print INST_DEF "#define BANKPSW (cpu8051_ReadD(_PSW_) & 0x18)\n\n";
+print INST_DEF "typedef int (*OPCODE_FP)(void);\n\n\n";
 for( $i=0; $i<256; $i++ ) {
     print INST_DEF "int\n";
-    print INST_DEF "cpu8051_OP_$a_opcodehex[$i]( void );\n\n";
+    print INST_DEF "cpu8051_OP_$a_opcodehex[$i](void);\n\n";
 }
 print INST_DEF "\n";
 print INST_DEF "/* Exported variables. */\n";
@@ -868,12 +872,7 @@ print INST_DEF "#endif\n\n\n";
 
 print INST_DEF "#endif /* INSTRUCTIONS_8051_H */\n";
 
-
-
-
-
-
-print DISASM_H "\n\n#endif /* DISASM_H */\n";
+print DISASM_H "#endif /* DISASM_H */\n";
 
 close DISASM_H;
 close OPCODELST;