home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / misc / emu / z80 / std_instr.i < prev    next >
Text File  |  1993-12-21  |  37KB  |  2,397 lines

  1. ** The non-implementation-dependent Z80 instructions
  2.  
  3. ** -----------------------------------------------------------------------
  4.  
  5. ** The address register named Work points to a longword called Workspace,
  6. ** which is used for fast temporary storage and byte swaps.
  7.  
  8. ** In order for the parity test to work, bits 8-15 of the tested register
  9. ** must be zero. The V flag (bit 1 of d6) must be clear before executing
  10. ** the macro.
  11.  
  12. ** -----------------------------------------------------------------------
  13.  
  14. ** Following opcodes exactly follow Zaks' "Programming the Z80".
  15. **
  16. **     n  = 8-bit number
  17. **     nn = 16-bit number
  18. **     e  = 8-bit relative jump offset
  19. **     d  = 8-bit displacement
  20. **     pq = 16-bit address
  21. **     p  = 8-bit address
  22. **
  23. ** Label notation:    Underscore used for SPACE, comma and +
  24. **
  25. **             1 (one) used for ( and )
  26.  
  27. ** =======================================================================
  28.  
  29.     IFD VERBOSE
  30.     LIST
  31. ** Compiling the std_instr.i file.
  32.     NOLIST
  33.     ENDC
  34.  
  35.  
  36. adc_A_r     MACRO
  37. Adc_A_\1
  38.     IFD BCDFLAGS
  39.         move.b    A,Z80_BCD_A(TableB)        ;BCD_A=A
  40.         move.b    \1,Z80_BCD_B(TableB)       ;BCD_B=r
  41.         move.w    d6,Z80_BCD_C(TableB)       ;BCD_C=carry
  42.         move.w    #OP_ADC,Z80_BCD_OP(TableB)   ;BCD_OP=adc
  43.     ENDC
  44.         lsr.w    #1,d6    ;carry to X
  45.         ori    #4,CCR    ;set Z
  46.         addx.b    \1,A
  47.         putsr    d6
  48.         next
  49.         ENDM
  50.     do_r adc_A_r
  51. ** --
  52. Adc_A_H     move.w    HL,(Work)
  53.         move.b    (Work),d7
  54.     IFD BCDFLAGS
  55.         move.b    A,Z80_BCD_A(TableB)
  56.         move.b    d7,Z80_BCD_B(TableB)
  57.         move.w    d6,Z80_BCD_C(TableB)
  58.         move.w    #OP_ADC,Z80_BCD_OP(TableB)
  59.     ENDC
  60.         lsr.w    #1,d6
  61.         ori    #4,CCR
  62.         addx.b    d7,A
  63.         putsr    d6
  64.         next
  65. ** --
  66. Adc_A_n     getRPC
  67.         getz    d7,d7
  68.     IFD BCDFLAGS
  69.         move.b    A,Z80_BCD_A(TableB)
  70.         move.b    d7,Z80_BCD_B(TableB)
  71.         move.w    d6,Z80_BCD_C(TableB)
  72.         move.w    #OP_ADC,Z80_BCD_OP(TableB)
  73.     ENDC
  74.         lsr.w    #1,d6
  75.         ori    #4,CCR
  76.         addx.b    d7,A
  77.         putsr    d6
  78.         skip 1
  79.         next
  80. ** --
  81. Adc_A_1HL1    getz    HL,d7
  82.     IFD BCDFLAGS
  83.         move.b    A,Z80_BCD_A(TableB)
  84.         move.b    d7,Z80_BCD_B(TableB)
  85.         move.w    d6,Z80_BCD_C(TableB)
  86.         move.w    #OP_ADC,Z80_BCD_OP(TableB)
  87.     ENDC
  88.         lsr.w    #1,d6
  89.         ori    #4,CCR
  90.         addx.b    d7,A
  91.         putsr    d6
  92.         next
  93. ** --
  94. adc_A_1xy1    MACRO
  95. Adc_A_1I\1_d1    opcode_2_bytes
  96.         index    \1
  97.         getz    d7,d7
  98.     IFD BCDFLAGS
  99.         move.b    A,Z80_BCD_A(TableB)
  100.         move.b    d7,Z80_BCD_B(TableB)
  101.         move.w    d6,Z80_BCD_C(TableB)
  102.         move.w    #OP_ADC,Z80_BCD_OP(TableB)
  103.     ENDC
  104.         lsr.w    #1,d6
  105.         ori    #4,CCR
  106.         addx.b    d7,A
  107.         putsr    d6
  108.         skip 2
  109.         next
  110.         ENDM
  111.     do_xy adc_A_1xy1
  112. ** --
  113. adc_HL_ss    MACRO
  114. Adc_HL_\1\2    opcode_2_bytes
  115.         join    \1,\2
  116.         lsr.w    #1,d6
  117.         ori    #4,CCR
  118.         addx.w    d7,HL
  119.         putsr    d6
  120.         skip 1
  121.         next
  122.         ENDM
  123.     do_ss adc_HL_ss
  124. ** --
  125. Adc_HL_HL    opcode_2_bytes
  126.         lsr.w    #1,d6
  127.         ori    #4,CCR
  128.         addx.w    HL,HL
  129.         putsr    d6
  130.         skip 1
  131.         next
  132. ** --
  133. Adc_HL_SP    opcode_2_bytes
  134.         move.w    ZSP,d7
  135.         lsr.w    #1,d6
  136.         ori    #4,CCR
  137.         addx.w    d7,HL
  138.         putsr    d6
  139.         skip 1
  140.         next
  141. ** --
  142. add_A_r     MACRO
  143. Add_A_\1
  144.     IFD BCDFLAGS
  145.         move.b    A,Z80_BCD_A(TableB)
  146.         move.b    \1,Z80_BCD_B(TableB)
  147.         clr.w    Z80_BCD_OP(TableB)
  148.     ENDC
  149.         add.b    \1,A
  150.         putsr    d6
  151.         next
  152.         ENDM
  153.     do_r add_A_r
  154. ** --
  155. Add_A_H     move.w    HL,(Work)
  156.         move.b    (Work),d7
  157.     IFD BCDFLAGS
  158.         move.b    A,Z80_BCD_A(TableB)
  159.         move.b    d7,Z80_BCD_B(TableB)
  160.         clr.w    Z80_BCD_OP(TableB)
  161.     ENDC
  162.         add.b    d7,A
  163.         putsr    d6
  164.         next
  165. ** --
  166. Add_A_n     getRPC
  167.         getz    d7,d7
  168.     IFD BCDFLAGS
  169.         move.b    A,Z80_BCD_A(TableB)
  170.         move.b    d7,Z80_BCD_B(TableB)
  171.         clr.w    Z80_BCD_OP(TableB)
  172.     ENDC
  173.         add.b    d7,A
  174.         putsr    d6
  175.         skip 1
  176.         next
  177. ** --
  178. Add_A_1HL1    getz    HL,d7
  179.     IFD BCDFLAGS
  180.         move.b    A,Z80_BCD_A(TableB)
  181.         move.b    d7,Z80_BCD_B(TableB)
  182.         clr.w    Z80_BCD_OP(TableB)
  183.     ENDC
  184.         add.b    d7,A
  185.         putsr    d6
  186.         next
  187. ** --
  188. add_A_1xy1    MACRO
  189. Add_A_1I\1_d1    opcode_2_bytes
  190.         index    \1
  191.         getz    d7,d7
  192.     IFD BCDFLAGS
  193.         move.b    A,Z80_BCD_A(TableB)
  194.         move.b    d7,Z80_BCD_B(TableB)
  195.         clr.w    Z80_BCD_OP(TableB)
  196.     ENDC
  197.         add.b    d7,A
  198.         putsr    d6
  199.         skip 2
  200.         next
  201.         ENDM
  202.     do_xy add_A_1xy1
  203. ** --
  204. add_HL_ss    MACRO
  205. Add_HL_\1\2    join    \1,\2
  206.         and.w    #%1110,d6
  207.         add.w    d7,HL
  208.         bcc.s    .cc
  209.         or.w    #1,d6
  210. .cc        next
  211.         ENDM
  212.     do_ss add_HL_ss
  213. ** --
  214. Add_HL_HL    and.w    #%1110,d6
  215.         add.w    HL,HL
  216.         bcc.s    .cc
  217.         or.w    #1,d6
  218. .cc        next
  219. ** --
  220. Add_HL_SP    and.w    #%1110,d6
  221.         add.w    ZSP,HL
  222.         bcc.s    .cc
  223.         or.w    #1,d6
  224. .cc        next
  225. ** --
  226. add_xy_ss    MACRO
  227. Add_I\1_\2\3    opcode_2_bytes
  228.         join    \2,\3
  229.         and.w    #%1110,d6
  230.         add.w    Z80_I\1(TableB),d7
  231.         bcc.s    .cc
  232.         or.w    #1,d6
  233. .cc        move.w    d7,Z80_I\1(TableB)
  234.         skip 1
  235.         next
  236.         ENDM
  237.  
  238. add_xy_xy    MACRO
  239. Add_I\1_I\1    opcode_2_bytes
  240.         and.w    #%1110,d6
  241.         move.w    Z80_I\1(TableB),d7
  242.         add.w    d7,d7
  243.         bcc.s    .cc
  244.         or.w    #1,d6
  245. .cc        move.w    d7,Z80_I\1(TableB)
  246.         skip 1
  247.         next
  248.         ENDM
  249.  
  250. add_xy_SP    MACRO
  251. Add_I\1_SP    opcode_2_bytes
  252.         and.w    #%1110,d6
  253.         move.w    Z80_I\1(TableB),d7
  254.         add.w    ZSP,d7
  255.         bcc.s    .cc
  256.         or.w    #1,d6
  257. .cc        move.w    d7,Z80_I\1(TableB)
  258.         skip 1
  259.         next
  260.         ENDM
  261.  
  262. addxy_xy    MACRO
  263.         add_xy_ss \1,B,C
  264.         add_xy_ss \1,D,E
  265.         add_xy_xy \1
  266.         add_xy_SP \1
  267.         ENDM
  268.     do_xy addxy_xy
  269. ** --
  270. and_r        MACRO
  271. And_\1        and.b    \1,A    ;V is cleared
  272.         putsr    d6
  273.         parity    A
  274.         next
  275.         ENDM
  276.     do_r and_r
  277. ** --
  278. And_H        move.w    HL,(Work)
  279.         and.b    (Work),A
  280.         putsr    d6
  281.         parity    A
  282.         next
  283. ** --
  284. And_n        getRPC
  285.         and.b    (Z0,d7.w),A
  286.         putsr    d6
  287.         parity    A
  288.         skip 1
  289.         next
  290. ** --
  291. And_1HL1    and.b    (Z0,HL.w),A
  292.         putsr    d6
  293.         parity    A
  294.         next
  295. ** --
  296. and_1xy1    MACRO
  297. And_1I\1_d1    opcode_2_bytes
  298.         index    \1
  299.         and.b    (Z0,d7.w),A
  300.         putsr    d6
  301.         parity    A
  302.         skip 2
  303.         next
  304.         ENDM
  305.     do_xy and_1xy1
  306. ** --
  307. bit_b_r     MACRO
  308. Bit_\1_\2    opcode_2_bytes
  309.         or.w    #%0100,d6
  310.         btst    #\1,\2
  311.         beq.s    .z
  312.         and.w    #%1011,d6
  313. .z        skip 1
  314.         next
  315.         ENDM
  316.  
  317. bit_b_H     MACRO
  318. Bit_\1_H    opcode_2_bytes
  319.         or.w    #%0100,d6
  320.         btst    #8+\1,HL
  321.         beq.s    .z
  322.         and.w    #%1011,d6
  323. .z        skip 1
  324.         next
  325.         ENDM
  326.  
  327. bit_b_HL    MACRO
  328. Bit_\1_1HL1    opcode_2_bytes
  329.         or.w    #%0100,d6
  330.         btst    #\1,(Z0,HL.w)
  331.         beq.s    .z
  332.         and.w    #%1011,d6
  333. .z        skip 1
  334.         next
  335.         ENDM
  336.  
  337. bit_b_1xy1    MACRO
  338. Bit_\1_1I\2_d1    opcode_3_bytes
  339.         index    \2
  340.         or.w    #%0100,d6
  341.         btst    #\1,(Z0,d7.w)
  342.         beq.s    .z
  343.         and.w    #%1011,d6
  344. .z        skip 3
  345.         next
  346.         ENDM
  347.  
  348. bit_b        MACRO
  349.     bit_b_r \1,A
  350.     bit_b_r \1,B
  351.     bit_b_r \1,C
  352.     bit_b_r \1,D
  353.     bit_b_r \1,E
  354.     bit_b_r \1,L
  355.     bit_b_H \1
  356.     bit_b_HL \1
  357.     bit_b_1xy1 \1,X
  358.     bit_b_1xy1 \1,Y
  359.         ENDM
  360.  
  361.     bit_b 0
  362.     bit_b 1
  363.     bit_b 2
  364.     bit_b 3
  365.     bit_b 4
  366.     bit_b 5
  367.     bit_b 6
  368.     bit_b 7
  369. ** --
  370. call_cc_pq    MACRO
  371. Call_\1_pq    skip 2
  372.         getsr
  373.         b\2.s    101$    ; 'exit' label in testreq macro
  374.         getRPC
  375.         move.w    d7,(Work)
  376.         decw    ZSP
  377.         putz    (Work),ZSP
  378.         decw    ZSP
  379.         putz    d7,ZSP,2 ;2nd use
  380.         decw    d7
  381.         getz    d7,(Work)
  382.         decw    d7
  383.         getz    d7,1(Work)
  384.         move.w    (Work),d7
  385.         makePPC
  386.         testreq
  387.         ENDM
  388.     call_cc_pq NZ,eq
  389.     call_cc_pq  Z,ne
  390.     call_cc_pq NC,cs
  391.     call_cc_pq  C,cc
  392.     call_cc_pq PO,vs
  393.     call_cc_pq PE,vc
  394.     call_cc_pq  P,mi
  395.     call_cc_pq  M,pl
  396. ** --
  397. Call_pq     getRPC
  398.         addq.w    #2,d7
  399.         move.w    d7,(Work)
  400.         decw    ZSP
  401.         putz    (Work),ZSP
  402.         decw    ZSP
  403.         putz    d7,ZSP,2 ;2nd use
  404.         decw    d7
  405.         getz    d7,(Work)
  406.         decw    d7
  407.         getz    d7,1(Work)
  408.         move.w    (Work),d7
  409.         makePPC
  410.         testreq
  411. ** --
  412. Ccf        eori.w    #1,d6
  413.         next
  414. ** --
  415. cp_r        MACRO
  416. Cp_\1        cmp.b    \1,A
  417.         putsr    d6
  418.         next
  419.         ENDM
  420.     do_r cp_r
  421. ** --
  422. Cp_H        move.w    HL,(Work)
  423.         cmp.b    (Work),A
  424.         putsr    d6
  425.         next
  426. ** --
  427. Cp_n        getRPC
  428.         cmp.b    (Z0,d7.w),A
  429.         putsr    d6
  430.         skip 1
  431.         next
  432. ** --
  433. Cp_1HL1     cmp.b    (Z0,HL.w),A
  434.         putsr    d6
  435.         next
  436. ** --
  437. cp_1xy1     MACRO
  438. Cp_1I\1_d1    opcode_2_bytes
  439.         index    \1
  440.         cmp.b    (Z0,d7.w),A
  441.         putsr    d6
  442.         skip 2
  443.         next
  444.         ENDM
  445.     do_xy cp_1xy1
  446. ** --
  447. Cpd        opcode_2_bytes
  448.         cmp.b    (Z0,HL.w),A
  449.         putsr    d7
  450.         eor.w    d7,d6    ;Join old carry and
  451.         and.w    #1,d6    ; new sign and zero
  452.         eor.w    d7,d6
  453.         or.w    #%0010,d6    ;V set if BC not zero
  454.         decw    HL    ;dec HL
  455.         decb    C
  456.         beq.s    .Cz
  457.         bcc.s    .BCnz    ;if carry, decrement B
  458.         decb    B
  459.         bra.s    .BCnz    ;C<>0, so BC<>0
  460.  
  461. .Cz        tst.b    B
  462.         bne.s    .BCnz
  463.         and.w    #%1101,d6    ;V reset if BC zero
  464. .BCnz        skip 1
  465.         next
  466. ** --
  467. Cpi        opcode_2_bytes
  468.         cmp.b    (Z0,HL.w),A
  469.         putsr    d7
  470.         eor.w    d7,d6    ;Join old carry and
  471.         and.w    #1,d6    ; new sign and zero
  472.         eor.w    d7,d6
  473.         or.w    #%0010,d6    ;V set if BC not zero
  474.         incw    HL    ;dec HL
  475.         decb    C
  476.         beq.s    .Cz
  477.         bcc.s    .BCnz    ;if carry, decrement B
  478.         decb    B
  479.         bra.s    .BCnz    ;C<>0, so BC<>0
  480.  
  481. .Cz        tst.b    B
  482.         bne.s    .BCnz
  483.         and.w    #%1101,d6    ;V reset if BC zero
  484. .BCnz        skip 1
  485.         next
  486. ** --
  487. Cpdr        opcode_2_bytes
  488.         move.b    B,(Work)
  489.         move.w    (Work),B
  490.         move.b    C,B    ;use B (word) as counter
  491. .loop        cmp.b    (Z0,HL.w),A
  492.         putsr    d7
  493.         beq.s    .eq
  494.         decw    HL    ;dec pointer
  495.         decw    B    ;dec counter
  496.         bne.s    .loop
  497. .BCz        and.w    #%1101,d7    ;BC zero, so reset V
  498. .end        eor.w    d7,d6        ;join old carry
  499.         and.w    #1,d6        ; with new N, Z & V
  500.         eor.w    d7,d6
  501.         move.b    B,C    ;BC=counter
  502.         lsr.w    #8,B    ;restore B, zeroing bits 15-8
  503.         skip 1
  504.         next
  505.  
  506. .eq        decw    HL    ;one last dec pointer
  507.         decw    B    ; and dec counter
  508.         beq.