1 ; Z80 emulator with CP/M support. The Z80-specific instructions themselves actually aren't
2 ; implemented yet, making this more of an i8080 emulator.
4 ; Copyright (C) 2010 Sprite_tm
6 ; This program is free software: you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation, either version 3 of the License, or
9 ; (at your option) any later version.
11 ; This program is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received a copy of the GNU General Public License
17 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #elif defined atmega168
26 .include "m168def.inc"
27 #elif defined atmega328P
28 .include "m328Pdef.inc"
40 #define F_CPU 20000000 /* system clock in Hz; defaults to 20MHz */
43 #define BAUD 38400 /* console baud rate */
46 #define PARTID 0x52 /* Partition table id */
47 /* http://www.win.tue.nl/~aeb/partitions/partition_types-1.html */
49 #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) /* clever rounding */
51 #define RXBUFSIZE 64 /* USART recieve buffer size. Must be power of 2 */
52 #define TXBUFSIZE 64 /* USART transmit buffer size. Must be power of 2 */
54 #define DRAM_WAITSTATES 1 /* Number of additional clock cycles for dram read access */
55 #define REFR_RATE 64000 /* dram refresh rate in cycles/s. */
56 /* Most drams need 1/15.6µs. */
57 #define REFR_PRE 8 /* timer prescale factor */
58 #define REFR_CS 0x02 /* timer clock select for 1/8 */
59 #define REFR_CNT F_CPU / REFR_RATE / REFR_PRE
63 #define DRAM_WORD_ACCESS 0 /* experimental */
65 #define EM_Z80 0 /* we don't have any z80 instructions yet */
94 .equ RAM_AH_MASK = (1<<ram_a8)|(1<<ram_a7)|(1<<ram_a6)|(1<<ram_a5)
95 .equ PD_OUTPUT_MASK = (1<<mmc_cs) | (1<<ram_oe) | RAM_AH_MASK
113 .equ RAM_AL_MASK = (1<<ram_a4)|(1<<ram_a3)|(1<<ram_a2)|(1<<ram_a1)|(1<<ram_a0)
114 .equ PB_OUTPUT_MASK = (1<<ram_ras) | RAM_AL_MASK
127 .equ RAM_DQ_MASK = (1<<ram_d3)|(1<<ram_d2)|(1<<ram_d1)|(1<<ram_d0)
128 .equ PC_OUTPUT_MASK = (1<<ram_cas)|(1<<ram_w)
131 ;Flag bits in z_flags
139 ;Register definitions
175 #if defined __ATmega8__
184 .equ hostact = 7 ;host active flag
185 .equ hostwrt = 6 ;host written flag
186 .equ rsflag = 5 ;read sector flag
187 .equ readop = 4 ;1 if read operation
190 ; This is the base z80 port address for clock access
191 #define TIMERPORT 0x40
192 #define TIMER_CTL TIMERPORT
193 #define TIMER_MSECS TIMERPORT+1
194 #define TIMER_SECS TIMER_MSECS+2
196 #define starttimercmd 1
197 #define quitTimerCmd 2
198 #define printTimerCmd 15
201 #if defined __ATmega8__
216 .equ OC2Aaddr= OC2addr
229 ;----------------------------------------
239 ;----------------------------------------
251 ;----------------------------------------
253 ; dram_wait number_of_cycles
268 rjmp start ; reset vector
270 rjmp refrint ; tim2cmpa
271 .org OC1Aaddr ; Timer/Counter1 Compare Match A
272 rjmp sysclockint ; 1ms system timer
274 rjmp rxint ; USART receive int.
276 rjmp txint ; USART transmit int.
278 .org INT_VECTORS_SIZE
281 ldi temp,low(RAMEND) ; top of memory
282 out SPL,temp ; init stack pointer
283 ldi temp,high(RAMEND) ; top of memory
284 out SPH,temp ; init stack pointer
292 ldi temp,(1<<WDCE) | (1<<WDE)
299 ldi zl,low(SRAM_START)
300 ldi zh,high(SRAM_START)
301 ldi temp2,high(ramtop)
309 ldi temp,(1<<PUD) ;disable pullups
312 out PORTD,temp ;all pins high
315 out DDRD,temp ; all outputs
327 ldi temp, (1<<TXEN0) | (1<<RXEN0) | (1<<RXCIE0)
330 ldi temp, (1<<URSEL) | (1<<UCSZ01) | (1<<UCSZ00)
332 ldi temp, (1<<UCSZ01) | (1<<UCSZ00)
335 ldi temp, HIGH(UBRR_VAL)
337 ldi temp, LOW(UBRR_VAL)
340 ; Init clock/timer system
342 ; Init timer 1 as 1 ms system clock tick.
344 ldi temp,high(F_CPU/1000)
346 ldi temp,low(F_CPU/1000)
348 ldi temp,(1<<WGM12) | (1<<CS10) ;CTC, clk/1
354 ;Init timer2. Refresh-call should happen every (8ms/512)=312 cycles.
356 ldi temp,REFR_CNT*2 ; 2 cycles per int
359 ori temp,(1<<WGM21) ;CTC mode
362 ori temp,REFR_CS ;clk/REFR_PRE
365 ori temp, (1<<OCIE2A)
378 .db "CPM on an AVR, v1.0",13,0,0
382 .db "Initing mmc...",13,0
388 .db "Testing RAM: fill...",0,0
410 .db "reread...",13,0,0
442 ;Fill ram with cbs, which (for now) will trigger an invalid opcode error.
454 ;----------------------------------------------------------------------------
461 ;Load first sector from MMC (boot sector)
470 ;Test, if it has a valid MBR
472 lds temp,hostbuf+510 ;MBR signature (0xAA55) at and of sector?
473 lds temp2,hostbuf+511
479 ;No MBR, no partition table ...
484 ;Search Partition Table for CP/M partitions
486 ldi zl,low(hostbuf+510-64)
487 ldi zh,high(hostbuf+510-64)
488 ldi yl,low(hostdstart)
489 ldi yh,high(hostdstart)
491 ldi oph,high(hostbuf+510)
497 ldd temp,z+PART_START
499 ldd temp2,z+PART_START+1
501 ldd temp3,z+PART_START+2
503 ldd temp4,z+PART_START+3
507 .db "CP/M partition at: ",0
512 ldd temp2,z+PART_SIZE+1
513 ldd temp3,z+PART_SIZE+2
514 ldd temp4,z+PART_SIZE+3
528 cpi zl,low(hostbuf+510)
535 ; Read first sector of first CP/M partition
548 .db "No bootable CP/M disk found! Please change MMC/SD-Card",13,0
560 ;First sector of disk or first CP/M partition is in hostbuf.
562 ;Save to Z80 RAM (only 128 bytes because that's retro)
571 cpi zl,low(hostbuf+128)
573 cpi zh,high(hostbuf+128)
575 rcall dsk_boot ;init (de)blocking buffer
586 .db 13,"Ok, CPU is live!",13,0,0
615 ; *** Stage 1: Fetch next opcode
638 ; *** Stage 2: Decode it using the ins_table.
639 ldi zh,high(inst_table*2)
660 ; *** Stage 3: Fetch operand. Use the fetch jumptable for this.
664 ldi zl,low(fetchjumps)
665 ldi zh,high(fetchjumps)
685 ; *** Stage 4: Execute operation :) Use the op jumptable for this.
701 .db ",post:oph:l=",0,0
710 ; *** Stage 5: Store operand. Use the store jumptable for this.
719 ldi zl,low(storejumps)
720 ldi zh,high(storejumps)
747 ; ----------------Virtual peripherial interface ------
749 ;The hw is modelled to make writing a CPM BIOS easier.
751 ;0 - Con status. Returns 0xFF if the UART has a byte, 0 otherwise.
752 ;1 - Console input, aka UDR.
755 ;16,17 - Track select
759 ;22 - Trigger - write to read, to write a sector using the above info;
760 ; , write to allocated/dirctory/unallocated
769 ;*****************************************************
770 ;* CP/M to host disk constants *
771 ;*****************************************************
772 .equ MAXDISKS = 4 ;Max number of Disks (partitions)
773 .equ blksize = 1024 ;CP/M allocation size
774 .equ hostsize = 512 ;host disk sector size
775 ; .equ hostspt = 20 ;host disk sectors/trk
776 .equ hostblk = hostsize/128 ;CP/M sects/host buff
777 ; .equ CPMSPT = hostblk*hostspt;CP/M sectors/track
779 .equ SECMSK = hostblk-1 ;sector mask
780 .equ SECSHF = log2(hostblk) ;sector shift
782 ;*****************************************************
783 ;* BDOS constants on entry to write *
784 ;*****************************************************
785 .equ WRALL = 0 ;write to allocated
786 .equ WRDIR = 1 ;write to directory
787 .equ WRUAL = 2 ;write to unallocated
788 .equ WRTMSK= 3 ;write type mask
792 ndisks: .byte 1 ;Number of CP/M disks
794 seekdsk: .byte 1 ;seek disk number
795 seektrk: .byte 2 ;seek track number
796 seeksec: .byte 1 ;seek sector number
798 hostdsk: .byte 1 ;host disk number
799 hostlba: .byte 3 ;host sector number (relative to partition start)
800 hostdstart: .byte 4*MAXDISKS ; host disks start sector (partition)
802 unacnt: .byte 1 ;unalloc rec cnt
803 unadsk: .byte 1 ;last unalloc disk
804 unatrk: .byte 2 ;last unalloc track
805 unasec: .byte 1 ;last unalloc sector
807 erflag: .byte 1 ;error reporting
808 wrtype: .byte 1 ;write operation type
809 dmaadr: .byte 2 ;last dma address
810 hostbuf: .byte hostsize;host buffer (from/to SD-card)
815 ;Called with port in temp2. Should return value in temp.
825 cpi temp2,TIMER_MSECS
827 cpi temp2,TIMER_MSECS+6
835 ;Called with port in temp2 and value in temp.
860 cpi temp2,TIMER_MSECS+6
893 lds temp2,ndisks ;check if selected disk # is less then # of disks
897 ldi temp,0xff ;error return
926 ;See what has to be done.
937 .db "DISK I/O: Invalid Function code: ",0
942 cbi flags,hostact ;host buffer inactive
943 sts unacnt,_0 ;clear unalloc count
947 sbis flags,hostwrt ;check for pending write
948 cbi flags,hostact ;clear host active flag
965 .db "Disk read: track ",0
985 sbi flags,readop ;read operation
986 sbi flags,rsflag ;must read data
987 ldi temp,WRUAL ;write type
988 sts wrtype,temp ;treat as unalloc
989 rjmp dsk_rwoper ;to perform the read
993 ;write the selected CP/M sector
996 sts wrtype,temp ;save write type
997 cbi flags,readop ;not a read operation
1004 .db "Disk write: track ",0,0
1014 .db " dma-addr ",0,0
1028 cpi temp,WRUAL ;write unallocated?
1029 brne dsk_chkuna ;check for unalloc
1031 ; write to unallocated, set parameters
1032 ldi temp,blksize/128 ;next unalloc recs
1034 lds temp,seekdsk ;disk to seek
1035 sts unadsk,temp ;unadsk = sekdsk
1037 sts unatrk,temp ;unatrk = sectrk
1039 sts unatrk+1,temp ;unatrk = sectrk
1041 sts unasec,temp ;unasec = seksec
1044 ;check for write to unallocated sector
1045 lds temp,unacnt ;any unalloc remain?
1047 breq dsk_alloc ;skip if not
1049 ; more unallocated records remain
1050 dec temp ;unacnt = unacnt-1
1052 lds temp,seekdsk ;same disk?
1054 cp temp,temp2 ;seekdsk = unadsk?
1055 brne dsk_alloc ;skip if not
1057 ; disks are the same
1062 cp temp,temp3 ;seektrk = unatrk?
1064 brne dsk_alloc ;skip if not
1066 ; tracks are the same
1067 lds temp,seeksec ;same sector?
1069 cp temp,temp2 ;seeksec = unasec?
1070 brne dsk_alloc ;skip if not
1072 ; match, move to next sector for future ref
1073 inc temp2 ;unasec = unasec+1
1075 cpi temp2,CPMSPT ;end of track? (count CP/M sectors)
1076 brlo dsk_noovf ;skip if no overflow
1078 ; overflow to next track
1079 sts unasec,_0 ;unasec = 0
1082 subi temp, low(-1) ;unatrk = unatrk+1
1088 cbi flags,rsflag ;rsflag = 0
1089 rjmp dsk_rwoper ;to perform the write
1092 ;not an unallocated record, requires pre-read
1093 sts unacnt,_0 ;unacnt = 0
1094 sbi flags,rsflag ;rsflag = 1
1096 ;*****************************************************
1097 ;* Common code for READ and WRITE follows *
1098 ;*****************************************************
1101 ;enter here to perform the read/write
1102 sts erflag,_0 ;no errors (yet)
1104 ;Convert track/sector to an LBA address (in 128byte blocks)
1110 lds temp4,seektrk+1 ;
1116 add xh,r0 ;xh:xl := sec + trk * SectorsPerTrack
1121 andi temp,SECMSK ;mask buffer number
1122 push temp ;save for later
1124 ;Convert from CP/M LBA blocks to host LBA blocks
1132 ;yl:xh:xl = host block to seek
1133 ; active host sector?
1134 sbis flags,hostact ;host active?
1135 rjmp dsk_filhst ;fill host if not
1137 ; host buffer active, same as seek buffer?
1139 lds temp2,hostdsk ;same disk?
1140 cp temp,temp2 ;seekdsk = hostdsk?
1143 ; same disk, same block?
1153 ;proper disk, but not correct sector
1154 sbic flags,hostwrt ;host written?
1155 rcall dsk_writehost ;clear host buff
1158 ;may have to fill the host buffer
1165 sbic flags,rsflag ;need to read?
1166 rcall dsk_readhost ;yes, if 1
1167 cbi flags,hostwrt ;no pending write
1170 sbi flags,hostact ;host buffer active now
1172 ;copy data to or from buffer
1174 ldi zh,high(hostbuf)
1176 pop temp2 ;get buffer number (which part of hostbuf)
1178 add zl,r0 ;offset in hostbuf
1185 .db "; host buf adr: ",0,0
1195 ldi temp3,128 ;length of move
1196 sbic flags,readop ;which way?
1197 rjmp dsk_rmove ;skip if read
1199 ; mark write operation
1200 sbi flags,hostwrt ;hostwrt = 1
1216 ; data has been moved to/from host buffer
1217 lds temp,wrtype ;write type
1218 cpi temp,WRDIR ;to directory?
1226 ret ;no further processing
1228 ; clear host buffer for directory write
1234 rcall dsk_writehost ;clear host buff
1235 cbi flags,hostwrt ;buffer written
1240 ;*****************************************************
1241 ;* WRITEhost performs the physical write to *
1242 ;* the host disk, READhost reads the physical *
1244 ;*****************************************************
1247 ;hostdsk = host disk #, hostlba = host block #.
1248 ;Write "hostsize" bytes from hostbuf and return
1249 ;error flag in erflag.
1250 ;Return erflag non-zero if error
1256 ldi zl,low(hostdstart)
1257 ldi zh,high(hostdstart)
1284 ;hostdsk = host disk #, hostlba = host block #.
1285 ;Read "hostsiz" bytes into hostbuf and return
1286 ;error flag in erflag.
1292 ldi zl,low(hostdstart)
1293 ldi zh,high(hostdstart)
1320 ;***************************************************************************
1322 ; ----------------- MMC/SD routines ------------------
1351 ;Wait till the mmc answers with the response in temp2, or till a timeout happens.
1358 brne mmcWaitResploopEnd
1362 rjmp mmcWaitResploop
1371 .db ": Error: MMC resp timeout!",13,0
1378 ;Init start: send 80 clocks with cs disabled
1382 ldi temp2,10 ; exactly 80 clocks
1404 ldi temp,0xff ;dummy
1406 ldi temp,0xff ;dummy
1420 ldi temp,0xff ;return byte
1423 ldi temp2,0 ;Error Code 0
1424 rcall mmcWaitResp ;Test on CMD0 is OK
1426 sbi P_MMC_CS,mmc_cs ;disable /CS
1430 ;Read OCR till card is ready
1431 ldi temp2,20 ;repeat counter
1435 cbi P_MMC_CS,mmc_cs ;enable /CS
1436 ldi temp,0xff ;dummy
1448 ; ldi temp,0x95 ;crc
1454 rcall mmcWaitResp ;wait until mmc-card send a byte <> 0xFF
1455 ;the first answer must be 0x01 (Idle-Mode)
1457 breq mmcInitOcrLoopDone ;second answer is 0x00 (Idle-Mode leave) CMD1 is OK
1459 sbi P_MMC_CS,mmc_cs ;disable /CS
1461 ; rcall mmcByteNoSend ;unnecessary
1469 brne mmcInitOcrLoop ;repeat
1476 sbi P_MMC_CS,mmc_cs ;disable /CS
1483 ;Call this with yh:yl:xh:xl = sector number
1491 ldi temp,0x51 ;cmd (read sector)
1493 lsl xl ;convert to byte address (*512)
1506 ldi temp,0xff ;return byte
1517 ;Read sector to AVR RAM
1519 ldi zh,high(hostbuf)
1523 cpi zl,low(hostbuf+512)
1525 cpi zh,high(hostbuf+512)
1539 ;Call this with yh:yl:xh:xl = sector number
1548 ldi temp,0x58 ;cmd (write sector)
1550 lsl xl ;convert to byte address (*512)
1563 ldi temp,0xff ;return byte
1574 ;Write sector from AVR RAM
1576 ldi zh,high(hostbuf)
1580 cpi zl,low(hostbuf+512)
1582 cpi zh,high(hostbuf+512)
1589 ;Status. Ignored for now.
1592 ;Wait till the mmc has written everything
1605 ;Set up wdt to time out after 1 sec.
1610 ldi temp,(1<<WDCE) | (1<<WDE) | (110<<WDP0)
1615 ; ------------------ DRAM routines -------------
1617 ; DRAM_SETADDR val, low_and_mask, low_or_mask, high_and_mask, high_or_mask
1632 ori temp, @4 | (1<<mmc_cs)
1636 ;Loads the byte on address xh:xl into temp.
1637 ;must not alter xh:xl
1641 DRAM_SETADDR xh, ~0,(1<<ram_ras), ~(1<<ram_a8), (1<<ram_oe)
1643 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_oe)), (1<<ram_a8)
1646 dram_wait DRAM_WAITSTATES ;
1647 in temp,P_DQ-2 ; PIN
1653 dram_wait DRAM_WAITSTATES ;
1654 in temp2,P_DQ-2 ; PIN
1664 #if DRAM_WORD_ACCESS
1679 DRAM_SETADDR xh, ~0,(1<<ram_ras), ~(1<<ram_a8),(1<<ram_oe)
1681 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_oe)), (1<<ram_a8)
1685 in temp,P_DQ-2 ; PIN
1691 in temp2,P_DQ-2 ; PIN
1699 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_oe)), (1<<ram_a8)
1703 in temp,P_DQ-2 ; PIN
1709 in temp2,P_DQ-2 ; PIN
1722 ;Writes the byte in temp to xh:xl
1723 ;must not alter xh:xl
1727 ldi temp2,RAM_DQ_MASK | (1<<ram_w) | (1<<ram_cas)
1731 andi temp,RAM_DQ_MASK & ~(1<<ram_w)
1732 ori temp,(1<<ram_cas)
1734 DRAM_SETADDR xh, ~0,(1<<ram_ras), ~(1<<ram_a8),(1<<ram_oe)
1736 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_a8)),(1<<ram_oe)
1743 andi temp2,RAM_DQ_MASK & ~(1<<ram_w)
1744 ori temp2,(1<<ram_cas)
1750 ldi temp,~RAM_DQ_MASK | (1<<ram_w) | (1<<ram_cas)
1756 #if DRAM_WORD_ACCESS
1771 ldi temp2,RAM_DQ_MASK | (1<<ram_w) | (1<<ram_cas)
1775 andi temp,RAM_DQ_MASK & ~(1<<ram_w)
1776 ori temp,(1<<ram_cas)
1779 DRAM_SETADDR xh, ~0,(1<<ram_ras), ~(1<<ram_a8),(1<<ram_oe)
1781 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_a8)),(1<<ram_oe)
1788 andi temp2,RAM_DQ_MASK & ~(1<<ram_w)
1789 ori temp2,(1<<ram_cas)
1798 andi temp,RAM_DQ_MASK & ~(1<<ram_w)
1799 ori temp,(1<<ram_cas)
1802 DRAM_SETADDR xl, ~(1<<ram_ras),0, ~((1<<ram_a8)),(1<<ram_oe)
1809 andi temp2,RAM_DQ_MASK & ~(1<<ram_w)
1810 ori temp2,(1<<ram_cas)
1816 ldi temp,~RAM_DQ_MASK | (1<<ram_w) | (1<<ram_cas)
1823 ; ****************************************************************************
1825 ; refresh interupt; exec 2 cbr cycles
1828 cbi P_CAS,ram_cas ;2 1| 1|
1830 cbi P_RAS,ram_ras ;2 |0 1|
1834 sbi P_RAS,ram_ras ;2 |0 |0
1836 dram_wait DRAM_WAITSTATES-1 ;
1838 cbi P_RAS,ram_ras ;2 |0 1|
1840 sbi P_CAS,ram_cas ;2 |0 |0
1842 sbi P_RAS,ram_ras ;2 1| |0
1844 reti ;4 --> 21 cycles
1847 ; ------------- system timer 1ms ---------------
1857 ; don't change order here, clock put/get depends on it.
1858 cntms_out: ; register for ms
1860 utime_io: ; register for uptime.
1867 .equ timer_size = timer_top - timer_base
1869 .equ clkofs = cnt_1ms-cntms_out
1870 .equ timerofs = cnt_1ms-timer_ms
1891 ldi zl,high(1000) ;doesn't change flags
1924 sts delay_timer,temp
1926 lds temp,delay_timer
1935 subi temp2,TIMER_MSECS
1936 brcs clkget_end ;Port number in range?
1937 ldi zl,low(cntms_out)
1938 ldi zh,high(cntms_out)
1939 breq clkget_copy ;lowest byte requestet, latch clock
1941 brsh clkget_end ;Port number to high?
1966 subi temp2,TIMERPORT
1967 brcs clkput_end ;Port number in range?
1972 cpi temp,starttimercmd
1974 cpi temp,quitTimerCmd
1976 cpi temp,printTimerCmd
1990 ldi zl,low(cntms_out)
1991 ldi zh,high(cntms_out)
1992 breq clkput_copy ;lowest byte requestet, latch clock
1994 brsh clkput_end ;Port number to high?
2019 ldi zl,low(timer_ms)
2020 ldi zh,high(timer_ms)
2038 ldi zl,low(timer_ms)
2039 ldi zh,high(timer_ms)
2041 ; put ms on stack (16 bit)
2063 ldd temp2,z+timerofs
2067 ldd temp3,z+timerofs
2072 ldd temp4,z+timerofs
2077 .db 13,"Timer running. Elapsed: ",0
2097 ldi zh,high(cnt_1ms)
2130 ; --------------- Debugging stuff ---------------
2132 ;Print a unsigned lonng value to the uart
2133 ; temp4:temp3:temp2:temp = value
2140 clr yl ;yl = stack level
2142 ultoa1: ldi z_flags, 32 ;yh = temp4:temp % 10
2143 clr yh ;temp4:temp /= 10
2155 cpi yh, 10 ;yh is a numeral digit '0'-'9'
2159 cp temp,_0 ;Repeat until temp4:temp gets zero
2166 ultoa5: cpi yl,3 ; at least 3 digits (ms)
2172 ultoa6: pop temp ;Flush stacked digits
2183 ;Prints the lower nibble of temp in hex to the uart
2199 ;Prints temp in hex to the uart
2207 ;Prints the zero-terminated string following the call statement.
2247 ; --------------- AVR HW <-> Z80 periph stuff ------------------
2249 .equ memReadByte = dram_read
2250 .equ memWriteByte = dram_write
2251 #if DRAM_WORD_ACCESS
2252 .equ memReadWord = dram_read_w
2253 .equ memWriteWord = dram_write_w
2256 ; --------------------------------------------------------------
2260 #define RXBUFMASK RXBUFSIZE-1
2261 #define TXBUFMASK TXBUFSIZE-1
2283 ; Save received character in a circular buffer. Do nothing if buffer overflows.
2292 lds zh,rxcount ;if rxcount < RXBUFSIZE
2293 cpi zh,RXBUFSIZE ; (room for at least 1 char?)
2296 sts rxcount,zh ; rxcount++
2298 ldi zl,low(rxfifo) ;
2303 sts rxidx_w,zh ; rxidx_w = ++rxidx_w % RXBUFSIZE
2304 ldi zh,high(rxfifo) ;
2307 st z,temp ; rxfifo[rxidx_w] = char
2317 ;Fetches a char from the buffer to temp. If none available, waits till one is.
2320 lds temp,rxcount ; Number of characters in buffer
2340 ld temp,z ;don't forget to get the char
2349 lds temp,txcount ;if txcount != 0
2354 sts txcount,temp ; --txcount
2357 ldi zl,low(txfifo) ;
2358 ldi zh,high(txfifo) ;
2364 andi temp,TXBUFMASK ;
2374 ldi temp, (1<<TXEN0) | (1<<RXEN0) | (1<<RXCIE0)
2383 ;Sends a char from temp to the uart.
2389 lds temp,txcount ;do {
2390 cpi temp,TXBUFSIZE ;
2391 brsh putc_l ;} while (txcount >= TXBUFSIZE)
2393 ldi zl,low(txfifo) ;
2394 ldi zh,high(txfifo) ;
2400 andi temp,TXBUFMASK ;
2401 sts txidx_w,temp ; txidx_w = ++txidx_w % TXBUFSIZE
2403 st z,temp ; txfifo[txidx_w] = char
2408 ldi zl, (1<<UDRIE0) | (1<<TXEN0) | (1<<RXEN0) | (1<<RXCIE0)
2415 ; ------------ Fetch phase stuff -----------------
2417 .equ FETCH_NOP = (0<<0)
2418 .equ FETCH_A = (1<<0)
2419 .equ FETCH_B = (2<<0)
2420 .equ FETCH_C = (3<<0)
2421 .equ FETCH_D = (4<<0)
2422 .equ FETCH_E = (5<<0)
2423 .equ FETCH_H = (6<<0)
2424 .equ FETCH_L = (7<<0)
2425 .equ FETCH_AF = (8<<0)
2426 .equ FETCH_BC = (9<<0)
2427 .equ FETCH_DE = (10<<0)
2428 .equ FETCH_HL = (11<<0)
2429 .equ FETCH_SP = (12<<0)
2430 .equ FETCH_MBC = (13<<0)
2431 .equ FETCH_MDE = (14<<0)
2432 .equ FETCH_MHL = (15<<0)
2433 .equ FETCH_MSP = (16<<0)
2434 .equ FETCH_DIR8 = (17<<0)
2435 .equ FETCH_DIR16= (18<<0)
2436 .equ FETCH_RST = (19<<0)
2439 ;Jump table for fetch routines. Make sure to keep this in sync with the .equs!
2534 #if DRAM_WORD_ACCESS
2555 #if DRAM_WORD_ACCESS
2580 ; ------------ Store phase stuff -----------------
2582 .equ STORE_NOP = (0<<5)
2583 .equ STORE_A = (1<<5)
2584 .equ STORE_B = (2<<5)
2585 .equ STORE_C = (3<<5)
2586 .equ STORE_D = (4<<5)
2587 .equ STORE_E = (5<<5)
2588 .equ STORE_H = (6<<5)
2589 .equ STORE_L = (7<<5)
2590 .equ STORE_AF = (8<<5)
2591 .equ STORE_BC = (9<<5)
2592 .equ STORE_DE = (10<<5)
2593 .equ STORE_HL = (11<<5)
2594 .equ STORE_SP = (12<<5)
2595 .equ STORE_PC = (13<<5)
2596 .equ STORE_MBC = (14<<5)
2597 .equ STORE_MDE = (15<<5)
2598 .equ STORE_MHL = (16<<5)
2599 .equ STORE_MSP = (17<<5)
2600 .equ STORE_RET = (18<<5)
2601 .equ STORE_CALL = (19<<5)
2602 .equ STORE_AM = (20<<5)
2604 ;Jump table for store routines. Make sure to keep this in sync with the .equs!
2700 #if DRAM_WORD_ACCESS
2741 ; ------------ Operation phase stuff -----------------
2744 .equ OP_NOP = (0<<10)
2745 .equ OP_INC = (1<<10)
2746 .equ OP_DEC = (2<<10)
2747 .equ OP_INC16 = (3<<10)
2748 .equ OP_DEC16 = (4<<10)
2749 .equ OP_RLC = (5<<10)
2750 .equ OP_RRC = (6<<10)
2751 .equ OP_RR = (7<<10)
2752 .equ OP_RL = (8<<10)
2753 .equ OP_ADDA = (9<<10)
2754 .equ OP_ADCA = (10<<10)
2755 .equ OP_SUBFA = (11<<10)
2756 .equ OP_SBCFA = (12<<10)
2757 .equ OP_ANDA = (13<<10)
2758 .equ OP_ORA = (14<<10)
2759 .equ OP_XORA = (15<<10)
2760 .equ OP_ADDHL = (16<<10)
2761 .equ OP_STHL = (17<<10) ;store HL in fetched address
2762 .equ OP_RMEM16 = (18<<10) ;read mem at fetched address
2763 .equ OP_RMEM8 = (19<<10) ;read mem at fetched address
2764 .equ OP_DA = (20<<10)
2765 .equ OP_SCF = (21<<10)
2766 .equ OP_CPL = (22<<10)
2767 .equ OP_CCF = (23<<10)
2768 .equ OP_POP16 = (24<<10)
2769 .equ OP_PUSH16 = (25<<10)
2770 .equ OP_IFNZ = (26<<10)
2771 .equ OP_IFZ = (27<<10)
2772 .equ OP_IFNC = (28<<10)
2773 .equ OP_IFC = (29<<10)
2774 .equ OP_IFPO = (30<<10)
2775 .equ OP_IFPE = (31<<10)
2776 .equ OP_IFP = (32<<10)
2777 .equ OP_IFM = (33<<10)
2778 .equ OP_OUTA = (34<<10)
2779 .equ OP_IN = (35<<10)
2780 .equ OP_EXHL = (36<<10)
2781 .equ OP_DI = (37<<10)
2782 .equ OP_EI = (38<<10)
2783 .equ OP_INV = (39<<10)
2784 .equ OP_CPFA = (40<<10)
2785 .equ OP_INCA = (41<<10)
2786 .equ OP_DECA = (42<<10)
2834 ;How the flags are supposed to work:
2835 ;7 ZFL_S - Sign flag (=MSBit of result)
2836 ;6 ZFL_Z - Zero flag. Is 1 when the result is 0
2837 ;4 ZFL_H - Half-carry (carry from bit 3 to 4)
2838 ;2 ZFL_P - Parity/2-complement Overflow
2839 ;1 ZFL_N - Subtract - set if last op was a subtract
2842 ;I sure hope I got the mapping between flags and instructions correct...
2844 ;----------------------------------------------------------------
2848 ;| ZZZZZZZ 88888 000 |
2854 ;| ZZZZZZZ 88888 000 |
2856 ;| Z80 MICROPROCESSOR Instruction Set Summary |
2858 ;----------------------------------------------------------------
2859 ;----------------------------------------------------------------
2860 ;|Mnemonic |SZHPNC|Description |Notes |
2861 ;|----------+------+---------------------+----------------------|
2862 ;|ADC A,s |***V0*|Add with Carry |A=A+s+CY |
2863 ;|ADC HL,ss |**?V0*|Add with Carry |HL=HL+ss+CY |
2864 ;|ADD A,s |***V0*|Add |A=A+s |
2865 ;|ADD HL,ss |--?-0*|Add |HL=HL+ss |
2866 ;|ADD IX,pp |--?-0*|Add |IX=IX+pp |
2867 ;|ADD IY,rr |--?-0*|Add |IY=IY+rr |
2868 ;|AND s |**1P00|Logical AND |A=A&s |
2869 ;|BIT b,m |?*1?0-|Test Bit |m&{2^b} |
2870 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
2871 ;|CALL nn |------|Unconditional Call |-[SP]=PC,PC=nn |
2872 ;|CCF |--?-0*|Complement Carry Flag|CY=~CY |
2873 ;|CP s |***V1*|Compare |A-s |
2874 ;|CPD |****1-|Compare and Decrement|A-[HL],HL=HL-1,BC=BC-1|
2875 ;|CPDR |****1-|Compare, Dec., Repeat|CPD till A=[HL]or BC=0|
2876 ;|CPI |****1-|Compare and Increment|A-[HL],HL=HL+1,BC=BC-1|
2877 ;|CPIR |****1-|Compare, Inc., Repeat|CPI till A=[HL]or BC=0|
2878 ;|CPL |--1-1-|Complement |A=~A |
2879 ;|DAA |***P-*|Decimal Adjust Acc. |A=BCD format |
2880 ;|DEC s |***V1-|Decrement |s=s-1 |
2881 ;|DEC xx |------|Decrement |xx=xx-1 |
2882 ;|DEC ss |------|Decrement |ss=ss-1 |
2883 ;|DI |------|Disable Interrupts | |
2884 ;|DJNZ e |------|Dec., Jump Non-Zero |B=B-1 till B=0 |
2885 ;|EI |------|Enable Interrupts | |
2886 ;|EX [SP],HL|------|Exchange |[SP]<->HL |
2887 ;|EX [SP],xx|------|Exchange |[SP]<->xx |
2888 ;|EX AF,AF' |------|Exchange |AF<->AF' |
2889 ;|EX DE,HL |------|Exchange |DE<->HL |
2890 ;|EXX |------|Exchange |qq<->qq' (except AF)|
2891 ;|HALT |------|Halt | |
2892 ;|IM n |------|Interrupt Mode | (n=0,1,2)|
2893 ;|IN A,[n] |------|Input |A=[n] |
2894 ;|IN r,[C] |***P0-|Input |r=[C] |
2895 ;|INC r |***V0-|Increment |r=r+1 |
2896 ;|INC [HL] |***V0-|Increment |[HL]=[HL]+1 |
2897 ;|INC xx |------|Increment |xx=xx+1 |
2898 ;|INC [xx+d]|***V0-|Increment |[xx+d]=[xx+d]+1 |
2899 ;|INC ss |------|Increment |ss=ss+1 |
2900 ;|IND |?*??1-|Input and Decrement |[HL]=[C],HL=HL-1,B=B-1|
2901 ;|INDR |?1??1-|Input, Dec., Repeat |IND till B=0 |
2902 ;|INI |?*??1-|Input and Increment |[HL]=[C],HL=HL+1,B=B-1|
2903 ;|INIR |?1??1-|Input, Inc., Repeat |INI till B=0 |
2904 ;|JP [HL] |------|Unconditional Jump |PC=[HL] |
2905 ;|JP [xx] |------|Unconditional Jump |PC=[xx] |
2906 ;|JP nn |------|Unconditional Jump |PC=nn |
2907 ;|JP cc,nn |------|Conditional Jump |If cc JP |
2908 ;|JR e |------|Unconditional Jump |PC=PC+e |
2909 ;|JR cc,e |------|Conditional Jump |If cc JR(cc=C,NC,NZ,Z)|
2910 ;|LD dst,src|------|Load |dst=src |
2911 ;|LD A,i |**0*0-|Load |A=i (i=I,R)|
2912 ;|LDD |--0*0-|Load and Decrement |[DE]=[HL],HL=HL-1,# |
2913 ;|LDDR |--000-|Load, Dec., Repeat |LDD till BC=0 |
2914 ;|LDI |--0*0-|Load and Increment |[DE]=[HL],HL=HL+1,# |
2915 ;|LDIR |--000-|Load, Inc., Repeat |LDI till BC=0 |
2916 ;|NEG |***V1*|Negate |A=-A |
2917 ;|NOP |------|No Operation | |
2918 ;|OR s |**0P00|Logical inclusive OR |A=Avs |
2919 ;|OTDR |?1??1-|Output, Dec., Repeat |OUTD till B=0 |
2920 ;|OTIR |?1??1-|Output, Inc., Repeat |OUTI till B=0 |
2921 ;|OUT [C],r |------|Output |[C]=r |
2922 ;|OUT [n],A |------|Output |[n]=A |
2923 ;|OUTD |?*??1-|Output and Decrement |[C]=[HL],HL=HL-1,B=B-1|
2924 ;|OUTI |?*??1-|Output and Increment |[C]=[HL],HL=HL+1,B=B-1|
2925 ;|POP xx |------|Pop |xx=[SP]+ |
2926 ;|POP qq |------|Pop |qq=[SP]+ |
2927 ;|PUSH xx |------|Push |-[SP]=xx |
2928 ;|PUSH qq |------|Push |-[SP]=qq |
2929 ;|RES b,m |------|Reset bit |m=m&{~2^b} |
2930 ;|RET |------|Return |PC=[SP]+ |
2931 ;|RET cc |------|Conditional Return |If cc RET |
2932 ;|RETI |------|Return from Interrupt|PC=[SP]+ |
2933 ;|RETN |------|Return from NMI |PC=[SP]+ |
2934 ;|RL m |**0P0*|Rotate Left |m={CY,m}<- |
2935 ;|RLA |--0-0*|Rotate Left Acc. |A={CY,A}<- |
2936 ;|RLC m |**0P0*|Rotate Left Circular |m=m<- |
2937 ;|RLCA |--0-0*|Rotate Left Circular |A=A<- |
2938 ;|RLD |**0P0-|Rotate Left 4 bits |{A,[HL]}={A,[HL]}<- ##|
2939 ;|RR m |**0P0*|Rotate Right |m=->{CY,m} |
2940 ;|RRA |--0-0*|Rotate Right Acc. |A=->{CY,A} |
2941 ;|RRC m |**0P0*|Rotate Right Circular|m=->m |
2942 ;|RRCA |--0-0*|Rotate Right Circular|A=->A |
2943 ;|RRD |**0P0-|Rotate Right 4 bits |{A,[HL]}=->{A,[HL]} ##|
2944 ;|RST p |------|Restart | (p=0H,8H,10H,...,38H)|
2945 ;|SBC A,s |***V1*|Subtract with Carry |A=A-s-CY |
2946 ;|SBC HL,ss |**?V1*|Subtract with Carry |HL=HL-ss-CY |
2947 ;|SCF |--0-01|Set Carry Flag |CY=1 |
2948 ;|SET b,m |------|Set bit |m=mv{2^b} |
2949 ;|SLA m |**0P0*|Shift Left Arithmetic|m=m*2 |
2950 ;|SRA m |**0P0*|Shift Right Arith. |m=m/2 |
2951 ;|SRL m |**0P0*|Shift Right Logical |m=->{0,m,CY} |
2952 ;|SUB s |***V1*|Subtract |A=A-s |
2953 ;|XOR s |**0P00|Logical Exclusive OR |A=Axs |
2954 ;|----------+------+--------------------------------------------|
2955 ;| F |-*01? |Flag unaffected/affected/reset/set/unknown |
2956 ;| S |S |Sign flag (Bit 7) |
2957 ;| Z | Z |Zero flag (Bit 6) |
2958 ;| HC | H |Half Carry flag (Bit 4) |
2959 ;| P/V | P |Parity/Overflow flag (Bit 2, V=overflow) |
2960 ;| N | N |Add/Subtract flag (Bit 1) |
2961 ;| CY | C|Carry flag (Bit 0) |
2962 ;|-----------------+--------------------------------------------|
2963 ;| n |Immediate addressing |
2964 ;| nn |Immediate extended addressing |
2965 ;| e |Relative addressing (PC=PC+2+offset) |
2966 ;| [nn] |Extended addressing |
2967 ;| [xx+d] |Indexed addressing |
2968 ;| r |Register addressing |
2969 ;| [rr] |Register indirect addressing |
2970 ;| |Implied addressing |
2971 ;| b |Bit addressing |
2972 ;| p |Modified page zero addressing (see RST) |
2973 ;|-----------------+--------------------------------------------|
2974 ;|DEFB n(,...) |Define Byte(s) |
2975 ;|DEFB 'str'(,...) |Define Byte ASCII string(s) |
2976 ;|DEFS nn |Define Storage Block |
2977 ;|DEFW nn(,...) |Define Word(s) |
2978 ;|-----------------+--------------------------------------------|
2979 ;| A B C D E |Registers (8-bit) |
2980 ;| AF BC DE HL |Register pairs (16-bit) |
2981 ;| F |Flag register (8-bit) |
2982 ;| I |Interrupt page address register (8-bit) |
2983 ;| IX IY |Index registers (16-bit) |
2984 ;| PC |Program Counter register (16-bit) |
2985 ;| R |Memory Refresh register |
2986 ;| SP |Stack Pointer register (16-bit) |
2987 ;|-----------------+--------------------------------------------|
2988 ;| b |One bit (0 to 7) |
2989 ;| cc |Condition (C,M,NC,NZ,P,PE,PO,Z) |
2990 ;| d |One-byte expression (-128 to +127) |
2991 ;| dst |Destination s, ss, [BC], [DE], [HL], [nn] |
2992 ;| e |One-byte expression (-126 to +129) |
2993 ;| m |Any register r, [HL] or [xx+d] |
2994 ;| n |One-byte expression (0 to 255) |
2995 ;| nn |Two-byte expression (0 to 65535) |
2996 ;| pp |Register pair BC, DE, IX or SP |
2997 ;| qq |Register pair AF, BC, DE or HL |
2998 ;| qq' |Alternative register pair AF, BC, DE or HL |
2999 ;| r |Register A, B, C, D, E, H or L |
3000 ;| rr |Register pair BC, DE, IY or SP |
3001 ;| s |Any register r, value n, [HL] or [xx+d] |
3002 ;| src |Source s, ss, [BC], [DE], [HL], nn, [nn] |
3003 ;| ss |Register pair BC, DE, HL or SP |
3004 ;| xx |Index register IX or IY |
3005 ;|-----------------+--------------------------------------------|
3006 ;| + - * / ^ |Add/subtract/multiply/divide/exponent |
3007 ;| & ~ v x |Logical AND/NOT/inclusive OR/exclusive OR |
3008 ;| <- -> |Rotate left/right |
3009 ;| [ ] |Indirect addressing |
3010 ;| [ ]+ -[ ] |Indirect addressing auto-increment/decrement|
3011 ;| { } |Combination of operands |
3012 ;| # |Also BC=BC-1,DE=DE-1 |
3013 ;| ## |Only lower 4 bits of accumulator A used |
3014 ;----------------------------------------------------------------
3025 ;------------------------------------------------;
3026 ; Move single bit between two registers
3028 ; bmov dstreg,dstbit,srcreg.srcbit
3036 ;------------------------------------------------;
3037 ; Load table value from flash indexed by source reg.
3039 ; ldpmx dstreg,tablebase,indexreg
3041 ; (6 words, 8 cycles)
3044 ldi zh,high(@1*2) ; table must be page aligned
3048 .macro do_z80_flags_HP
3050 bmov z_flags, ZFL_P, temp, AVR_V
3051 bmov z_flags, ZFL_H, temp, AVR_H
3055 .macro do_z80_flags_set_N
3057 ori z_flags, (1<<ZFL_N) ; Negation auf 1
3061 .macro do_z80_flags_set_HN
3063 ori z_flags,(1<<ZFL_N)|(1<<ZFL_H)
3067 .macro do_z80_flags_clear_N
3069 andi z_flags,~(1<<ZFL_N)
3073 .macro do_z80_flags_op_rotate
3074 ; must not change avr carry flag!
3076 andi z_flags, ~( (1<<ZFL_H) | (1<<ZFL_N) | (1<<ZFL_C) )
3078 andi z_flags, ~( (1<<ZFL_C) )
3082 .macro do_z80_flags_op_and
3084 ori z_flags,(1<<ZFL_H)
3086 ori z_flags,(1<<ZFL_H)
3090 .macro do_z80_flags_op_or
3099 ;----------------------------------------------------------------
3100 ;|Mnemonic |SZHPNC|Description |Notes |
3101 ;----------------------------------------------------------------
3102 ;|INC r |***V0-|Increment |r=r+1 |
3103 ;|INC [HL] |***V0-|Increment |[HL]=[HL]+1 |
3104 ;|INC [xx+d]|***V0-|Increment |[xx+d]=[xx+d]+1 |
3105 ;|----------|SZHP C|---------- 8080 ----------------------------|
3106 ;|INC r |**-P0-|Increment |r=r+1 |
3107 ;|INC [HL] |**-P0-|Increment |[HL]=[HL]+1 |
3115 andi z_flags,(1<<ZFL_H)|(1<<ZFL_C) ; preserve C-, and H-flag
3116 ldpmx temp2, sz53p_tab, opl
3126 andi z_flags,(1<<ZFL_H)|(1<<ZFL_C) ; preserve C-, and H-flag
3127 ldpmx temp2, sz53p_tab, z_a
3132 ;----------------------------------------------------------------
3133 ;|Mnemonic |SZHPNC|Description |Notes |
3134 ;----------------------------------------------------------------
3135 ;|DEC r |***V1-|Decrement |s=s-1 |
3136 ;|INC [HL] |***V0-|Increment |[HL]=[HL]+1 |
3137 ;|INC [xx+d]|***V0-|Increment |[xx+d]=[xx+d]+1 |
3138 ;|----------|SZHP C|---------- 8080 ----------------------------|
3139 ;|DEC r |**-P -|Increment |r=r+1 |
3140 ;|DEC [HL] |**-P -|Increment |[HL]=[HL]+1 |
3148 andi z_flags,(1<<ZFL_H)|(1<<ZFL_C) ; preserve C-, and H-flag
3149 ldpmx temp2, sz53p_tab, opl
3160 andi z_flags,(1<<ZFL_H)|(1<<ZFL_C) ; preserve C-, and H-flag
3161 ldpmx temp2, sz53p_tab, z_a
3167 ;----------------------------------------------------------------
3168 ;|Mnemonic |SZHPNC|Description |Notes |
3169 ;----------------------------------------------------------------
3170 ;|INC xx |------|Increment |xx=xx+1 |
3171 ;|INC ss |------|Increment |ss=ss+1 |
3179 ;----------------------------------------------------------------
3180 ;|Mnemonic |SZHPNC|Description |Notes |
3181 ;----------------------------------------------------------------
3182 ;|DEC xx |------|Decrement |xx=xx-1 |
3183 ;|DEC ss |------|Decrement |ss=ss-1 |
3191 ;----------------------------------------------------------------
3192 ;|Mnemonic |SZHPNC|Description |Notes |
3193 ;----------------------------------------------------------------
3194 ;|RLCA |--0-0*|Rotate Left Circular |A=A<- |
3195 ;|----------|SZHP C|---------- 8080 ----------------------------|
3196 ;|RLCA |---- *|Rotate Left Circular |A=A<- |
3200 ;Rotate Left Cyclical. All bits move 1 to the
3201 ;left, the msb becomes c and lsb.
3202 do_z80_flags_op_rotate
3206 ori z_flags, (1<<ZFL_C)
3210 ;----------------------------------------------------------------
3211 ;|Mnemonic |SZHPNC|Description |Notes |
3212 ;----------------------------------------------------------------
3213 ;|RRCA |--0-0*|Rotate Right Circular|A=->A |
3214 ;|----------|SZHP C|---------- 8080 ----------------------------|
3215 ;|RRCA |---- *|Rotate Right Circular|A=->A |
3219 ;Rotate Right Cyclical. All bits move 1 to the
3220 ;right, the lsb becomes c and msb.
3221 do_z80_flags_op_rotate
3225 ori z_flags, (1<<ZFL_C)
3229 ;----------------------------------------------------------------
3230 ;|Mnemonic |SZHPNC|Description |Notes |
3231 ;----------------------------------------------------------------
3232 ;|RRA |--0-0*|Rotate Right Acc. |A=->{CY,A} |
3233 ;|----------|SZHP C|---------- 8080 ----------------------------|
3234 ;|RRA |---- *|Rotate Right Acc. |A=->{CY,A} |
3238 ;Rotate Right. All bits move 1 to the right, the lsb
3239 ;becomes c, c becomes msb.
3240 clc ; get z80 carry to avr carry
3243 do_z80_flags_op_rotate ; (clear ZFL_C, doesn't change AVR_C)
3244 bmov z_flags,ZFL_C, opl,0 ; Bit 0 --> CY
3248 ;----------------------------------------------------------------
3249 ;|Mnemonic |SZHPNC|Description |Notes |
3250 ;----------------------------------------------------------------
3251 ;|RLA |--0-0*|Rotate Left Acc. |A={CY,A}<- |
3252 ;|----------|SZHP C|---------- 8080 ----------------------------|
3253 ;|RLA |---- *|Rotate Left Acc. |A={CY,A}<- |
3257 ;Rotate Left. All bits move 1 to the left, the msb
3258 ;becomes c, c becomes lsb.
3262 do_z80_flags_op_rotate ; (clear ZFL_C, doesn't change AVR_C)
3263 bmov z_flags,ZFL_C, opl,7 ; Bit 7 --> CY
3267 ;----------------------------------------------------------------
3268 ;|Mnemonic |SZHPNC|Description |Notes |
3269 ;----------------------------------------------------------------
3270 ;|ADD A,s |***V0*|Add |A=A+s |
3271 ;|----------|SZHP C|---------- 8080 ----------------------------|
3272 ;|ADD A,s |***P *|Add |A=A+s |
3278 ldpmx z_flags,sz53p_tab,z_a ;S,Z,P flag
3279 bmov z_flags,ZFL_C, temp,AVR_C
3283 ;----------------------------------------------------------------
3284 ;|Mnemonic |SZHPNC|Description |Notes |
3285 ;----------------------------------------------------------------
3286 ;|ADC A,s |***V0*|Add with Carry |A=A+s+CY |
3287 ;|----------|SZHP C|---------- 8080 ----------------------------|
3288 ;|ADC A,s |***P *|Add with Carry |A=A+s+CY |
3297 ldpmx z_flags,sz53p_tab,z_a ;S,Z,P
3298 bmov z_flags,ZFL_C, temp,AVR_C
3302 ;----------------------------------------------------------------
3303 ;|Mnemonic |SZHPNC|Description |Notes |
3304 ;----------------------------------------------------------------
3305 ;|SUB s |***V1*|Subtract |A=A-s |
3306 ;|----------|SZHP C|---------- 8080 ----------------------------|
3307 ;|SUB s |***P *|Subtract |A=A-s |
3313 ldpmx z_flags,sz53p_tab,z_a ;S,Z,P
3314 bmov z_flags,ZFL_C, temp,AVR_C
3319 ;----------------------------------------------------------------
3320 ;|Mnemonic |SZHPNC|Description |Notes |
3321 ;----------------------------------------------------------------
3322 ;|CP s |***V1*|Compare |A-s |
3323 ;|----------|SZHP C|---------- 8080 ----------------------------|
3324 ;|CP s |***P *|Compare |A-s |
3332 ldpmx z_flags,sz53p_tab,opl ;S,Z,P
3333 bmov z_flags,ZFL_C, temp,AVR_C
3338 ;----------------------------------------------------------------
3339 ;|Mnemonic |SZHPNC|Description |Notes |
3340 ;----------------------------------------------------------------
3341 ;|SBC A,s |***V1*|Subtract with Carry |A=A-s-CY |
3342 ;|----------|SZHP C|---------- 8080 ----------------------------|
3343 ;|SBC A,s |***P *|Subtract with Carry |A=A-s-CY |
3352 ldpmx z_flags,sz53p_tab,z_a ;S,Z,P
3353 bmov z_flags,ZFL_C, temp,AVR_C
3358 ;----------------------------------------------------------------
3359 ;|Mnemonic |SZHPNC|Description |Notes |
3360 ;----------------------------------------------------------------
3361 ;|AND s |**1P00|Logical AND |A=A&s |
3362 ;|----------|SZHP C|---------- 8080 ----------------------------|
3363 ;|AND s |**-P 0|Logical AND |A=A&s |
3368 ldpmx z_flags,sz53p_tab,z_a ;S,Z,P,N,C
3373 ;----------------------------------------------------------------
3374 ;|Mnemonic |SZHPNC|Description |Notes |
3375 ;----------------------------------------------------------------
3376 ;|OR s |**0P00|Logical inclusive OR |A=Avs |
3377 ;|----------|SZHP C|---------- 8080 ----------------------------|
3378 ;|OR s |**-P00|Logical inclusive OR |A=Avs |
3383 ldpmx z_flags,sz53p_tab,z_a ;S,Z,H,P,N,C
3387 ;----------------------------------------------------------------
3388 ;|Mnemonic |SZHPNC|Description |Notes |
3389 ;----------------------------------------------------------------
3390 ;|XOR s |**0P00|Logical Exclusive OR |A=Axs |
3391 ;|----------|SZHP C|---------- 8080 ----------------------------|
3392 ;|XOR s |**-P 0|Logical Exclusive OR |A=Axs |
3397 ldpmx z_flags,sz53p_tab,z_a ;S,Z,H,P,N,C
3401 ;----------------------------------------------------------------
3402 ;|Mnemonic |SZHPNC|Description |Notes |
3403 ;----------------------------------------------------------------
3404 ;|ADD HL,ss |--?-0*|Add |HL=HL+ss |
3405 ;|----------|SZHP C|---------- 8080 ----------------------------|
3406 ;|ADD HL,ss |---- *|Add |HL=HL+ss |
3413 bmov z_flags,ZFL_H, temp,AVR_H
3414 bmov z_flags,ZFL_C, temp,AVR_C
3415 do_z80_flags_clear_N
3418 ;----------------------------------------------------------------
3419 ;|Mnemonic |SZHPNC|Description |Notes |
3420 ;----------------------------------------------------------------
3421 ;|LD dst,src|------|Load |dst=src |
3424 do_op_sthl: ;store hl to mem loc in opl:h
3426 #if DRAM_WORD_ACCESS
3438 ;----------------------------------------------------------------
3439 ;|Mnemonic |SZHPNC|Description |Notes |
3440 ;----------------------------------------------------------------
3441 ;|LD dst,src|------|Load |dst=src |
3446 #if DRAM_WORD_ACCESS
3458 ;----------------------------------------------------------------
3459 ;|Mnemonic |SZHPNC|Description |Notes |
3460 ;----------------------------------------------------------------
3461 ;|LD dst,src|------|Load |dst=src |
3470 ;----------------------------------------------------------------
3471 ;|Mnemonic |SZHPNC|Description |Notes |
3472 ;----------------------------------------------------------------
3473 ;|DAA |***P-*|Decimal Adjust Acc. | |
3474 ;|----------|SZHP C|---------- 8080 ----------------------------|
3478 ; Description (http://www.z80.info/z80syntx.htm#DAA):
3479 ; This instruction conditionally adjusts the accumulator for BCD addition
3480 ; and subtraction operations. For addition (ADD, ADC, INC) or subtraction
3481 ; (SUB, SBC, DEC, NEC), the following table indicates the operation performed:
3483 ; -------------------------------------------------------------------------------
3484 ; | | C Flag | HEX value in | H Flag | HEX value in | Number | C flag|
3485 ; | Operation| Before | upper digit | Before | lower digit | added | After |
3486 ; | | DAA | (bit 7-4) | DAA | (bit 3-0) | to byte | DAA |
3487 ; |-----------------------------------------------------------------------------|
3488 ; | | 0 | 0-9 | 0 | 0-9 | 00 | 0 |
3489 ; | ADD | 0 | 0-8 | 0 | A-F | 06 | 0 |
3490 ; | | 0 | 0-9 | 1 | 0-3 | 06 | 0 |
3491 ; | ADC | 0 | A-F | 0 | 0-9 | 60 | 1 |
3492 ; | | 0 | 9-F | 0 | A-F | 66 | 1 |
3493 ; | INC | 0 | A-F | 1 | 0-3 | 66 | 1 |
3494 ; | | 1 | 0-2 | 0 | 0-9 | 60 | 1 |
3495 ; | | 1 | 0-2 | 0 | A-F | 66 | 1 |
3496 ; | | 1 | 0-3 | 1 | 0-3 | 66 | 1 |
3497 ; |-----------------------------------------------------------------------------|
3498 ; | SUB | 0 | 0-9 | 0 | 0-9 | 00 | 0 |
3499 ; | SBC | 0 | 0-8 | 1 | 6-F | FA | 0 |
3500 ; | DEC | 1 | 7-F | 0 | 0-9 | A0 | 1 |
3501 ; | NEG | 1 | 6-F | 1 | 6-F | 9A | 1 |
3502 ; |-----------------------------------------------------------------------------|
3505 ; C: See instruction.
3507 ; P/V: Set if Acc. is even parity after operation, reset otherwise.
3508 ; H: See instruction.
3509 ; Z: Set if Acc. is Zero after operation, reset otherwise.
3510 ; S: Set if most significant bit of Acc. is 1 after operation, reset otherwise.
3516 ldi oph,0 ; what to add
3517 sbrc z_flags,ZFL_H ; if H-Flag
3520 andi temp,0x0f ; ... or lower digit > 9
3526 sbrc z_flags,(1<<ZFL_C)
3535 ori z_flags,(1<<ZFL_C); set C
3537 sbrs z_flags,ZFL_N ; if sub-op
3538 rjmp op_da_add ; then
3541 op_da_add: ; else add-op
3554 ori z_flags,(1<<ZFL_C)
3555 andi z_flags,(1<<ZFL_N)|(1<<ZFL_C) ; preserve C,N
3556 ldpmx temp2, sz53p_tab, opl ; get S,Z,P
3558 bmov z_flags,ZFL_H, temp,AVR_H ; H (?)
3563 sbrc z_flags,ZFL_N ; if add-op
3564 rjmp do_op_da_sub ; then
3568 cpi temp,0x0a ; if lower digit > 9
3570 ori temp2,0x06 ; add 6 to lower digit
3572 sbrc z_flags,ZFL_H ; ... or H-Flag
3582 do_op_da_c: ; else sub-op
3583 sbrc z_flags,ZFL_C ;
3585 andi z_flags, ~( (1<<ZFL_S) | (1<<ZFL_Z) | (1<<ZFL_H) )
3588 bst temp,AVR_Z ;Z-Flag
3590 bst temp,AVR_N ;S-Flag
3592 sbrc temp2,5 ;C-Flag, set if 0x06 added
3593 ori z_flags,(1<<ZFL_C) ;
3597 do_op_da_sub: ;TODO:
3602 ;----------------------------------------------------------------
3603 ;|Mnemonic |SZHPNC|Description |Notes |
3604 ;----------------------------------------------------------------
3605 ;|SCF |--0-01|Set Carry Flag |CY=1 |
3606 ;|----------|SZHP C|---------- 8080 ----------------------------|
3610 andi z_flags,~((1<<ZFL_H)|(1<<ZFL_N))
3611 ori z_flags,(1<<ZFL_C)
3614 ;----------------------------------------------------------------
3615 ;|Mnemonic |SZHPNC|Description |Notes |
3616 ;----------------------------------------------------------------
3617 ;|CCF |--?-0*|Complement Carry Flag|CY=~CY |
3618 ;|----------|SZHP C|---------- 8080 ----------------------------|
3619 ;|SCF |---- 1|Set Carry Flag |CY=1 |
3623 do_z80_flags_clear_N
3628 ;----------------------------------------------------------------
3629 ;|Mnemonic |SZHPNC|Description |Notes |
3630 ;----------------------------------------------------------------
3631 ;|CPL |--1-1-|Complement |A=~A |
3632 ;|----------|SZHP C|---------- 8080 ----------------------------|
3633 ;|CPL |---- -|Complement |A=~A |
3642 ;----------------------------------------------------------------
3643 ;|Mnemonic |SZHPNC|Description |Notes |
3644 ;----------------------------------------------------------------
3645 ;|PUSH xx |------|Push |-[SP]=xx |
3646 ;|PUSH qq |------|Push |-[SP]=qq |
3654 #if DRAM_WORD_ACCESS
3673 .db ", SP is now ",0
3684 ;----------------------------------------------------------------
3685 ;|Mnemonic |SZHPNC|Description |Notes |
3686 ;----------------------------------------------------------------
3687 ;|POP xx |------|Pop |xx=[SP]+ |
3688 ;|POP qq |------|Pop |qq=[SP]+ |
3693 #if DRAM_WORD_ACCESS
3711 .db "Stack pop: val ",0
3727 ;----------------------------------------------------------------
3728 ;|Mnemonic |SZHPNC|Description |Notes |
3729 ;----------------------------------------------------------------
3730 ;|EX [SP],HL|------|Exchange |[SP]<->HL |
3731 ;|EX DE,HL |------|Exchange |DE<->HL |
3743 ;----------------------------------------------------------------
3744 ;|Mnemonic |SZHPNC|Description |Notes |
3745 ;----------------------------------------------------------------
3747 ; TODO: Implement IFF1, IFF2
3751 ;----------------------------------------------------------------
3752 ;|Mnemonic |SZHPNC|Description |Notes |
3753 ;----------------------------------------------------------------
3755 ; TODO: Implement IFF1, IFF2
3759 ;----------------------------------------------------------------
3760 ;|Mnemonic |SZHPNC|Description |Notes |
3761 ;----------------------------------------------------------------
3762 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3763 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3764 ;|RET cc |------|Conditional Return |If cc RET |
3774 ;----------------------------------------------------------------
3775 ;|Mnemonic |SZHPNC|Description |Notes |
3776 ;----------------------------------------------------------------
3777 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3778 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3779 ;|RET cc |------|Conditional Return |If cc RET |
3789 ;----------------------------------------------------------------
3790 ;|Mnemonic |SZHPNC|Description |Notes |
3791 ;----------------------------------------------------------------
3792 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3793 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3794 ;|RET cc |------|Conditional Return |If cc RET |
3804 ;----------------------------------------------------------------
3805 ;|Mnemonic |SZHPNC|Description |Notes |
3806 ;----------------------------------------------------------------
3807 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3808 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3809 ;|RET cc |------|Conditional Return |If cc RET |
3819 ;----------------------------------------------------------------
3820 ;|Mnemonic |SZHPNC|Description |Notes |
3821 ;----------------------------------------------------------------
3822 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3823 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3824 ;|RET cc |------|Conditional Return |If cc RET |
3834 ;----------------------------------------------------------------
3835 ;|Mnemonic |SZHPNC|Description |Notes |
3836 ;----------------------------------------------------------------
3837 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3838 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3839 ;|RET cc |------|Conditional Return |If cc RET |
3849 ;----------------------------------------------------------------
3850 ;|Mnemonic |SZHPNC|Description |Notes |
3851 ;----------------------------------------------------------------
3852 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3853 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3854 ;|RET cc |------|Conditional Return |If cc RET |
3857 do_op_ifp: ;sign positive, aka s=0
3864 ;----------------------------------------------------------------
3865 ;|Mnemonic |SZHPNC|Description |Notes |
3866 ;----------------------------------------------------------------
3867 ;|CALL cc,nn|------|Conditional Call |If cc CALL |
3868 ;|JP cc,nn |------|Conditional Jump |If cc JP |
3869 ;|RET cc |------|Conditional Return |If cc RET |
3872 do_op_ifm: ;sign negative, aka s=1
3879 ;----------------------------------------------------------------
3880 ;|Mnemonic |SZHPNC|Description |Notes |
3881 ;----------------------------------------------------------------
3882 ;|OUT [n],A |------|Output |[n]=A |
3885 ;Interface with peripherials goes here :)
3886 do_op_outa: ; out (opl),a
3889 .db 13,"Port write: ",0
3904 ;----------------------------------------------------------------
3905 ;|Mnemonic |SZHPNC|Description |Notes |
3906 ;----------------------------------------------------------------
3907 ;|IN A,[n] |------|Input |A=[n] |
3910 do_op_in: ; in a,(opl)
3913 .db 13,"Port read: (",0
3932 ;----------------------------------------------------------------
3935 .db "Invalid opcode @ PC=",0,0
3941 ;----------------------------------------------------------------
3945 ;----------------------------------------------------------------
3946 ; Lookup table, stolen from z80ex, Z80 emulation library.
3947 ; http://z80ex.sourceforge.net/
3949 ; The S, Z, 5 and 3 bits and the parity of the lookup value
3950 .org (PC+255) & 0xff00
3952 .db 0x44,0x00,0x00,0x04,0x00,0x04,0x04,0x00
3953 .db 0x08,0x0c,0x0c,0x08,0x0c,0x08,0x08,0x0c
3954 .db 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04
3955 .db 0x0c,0x08,0x08,0x0c,0x08,0x0c,0x0c,0x08
3956 .db 0x20,0x24,0x24,0x20,0x24,0x20,0x20,0x24
3957 .db 0x2c,0x28,0x28,0x2c,0x28,0x2c,0x2c,0x28
3958 .db 0x24,0x20,0x20,0x24,0x20,0x24,0x24,0x20
3959 .db 0x28,0x2c,0x2c,0x28,0x2c,0x28,0x28,0x2c
3960 .db 0x00,0x04,0x04,0x00,0x04,0x00,0x00,0x04
3961 .db 0x0c,0x08,0x08,0x0c,0x08,0x0c,0x0c,0x08
3962 .db 0x04,0x00,0x00,0x04,0x00,0x04,0x04,0x00
3963 .db 0x08,0x0c,0x0c,0x08,0x0c,0x08,0x08,0x0c
3964 .db 0x24,0x20,0x20,0x24,0x20,0x24,0x24,0x20
3965 .db 0x28,0x2c,0x2c,0x28,0x2c,0x28,0x28,0x2c
3966 .db 0x20,0x24,0x24,0x20,0x24,0x20,0x20,0x24
3967 .db 0x2c,0x28,0x28,0x2c,0x28,0x2c,0x2c,0x28
3968 .db 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84
3969 .db 0x8c,0x88,0x88,0x8c,0x88,0x8c,0x8c,0x88
3970 .db 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80
3971 .db 0x88,0x8c,0x8c,0x88,0x8c,0x88,0x88,0x8c
3972 .db 0xa4,0xa0,0xa0,0xa4,0xa0,0xa4,0xa4,0xa0
3973 .db 0xa8,0xac,0xac,0xa8,0xac,0xa8,0xa8,0xac
3974 .db 0xa0,0xa4,0xa4,0xa0,0xa4,0xa0,0xa0,0xa4
3975 .db 0xac,0xa8,0xa8,0xac,0xa8,0xac,0xac,0xa8
3976 .db 0x84,0x80,0x80,0x84,0x80,0x84,0x84,0x80
3977 .db 0x88,0x8c,0x8c,0x88,0x8c,0x88,0x88,0x8c
3978 .db 0x80,0x84,0x84,0x80,0x84,0x80,0x80,0x84
3979 .db 0x8c,0x88,0x88,0x8c,0x88,0x8c,0x8c,0x88
3980 .db 0xa0,0xa4,0xa4,0xa0,0xa4,0xa0,0xa0,0xa4
3981 .db 0xac,0xa8,0xa8,0xac,0xa8,0xac,0xac,0xa8
3982 .db 0xa4,0xa0,0xa0,0xa4,0xa0,0xa4,0xa4,0xa0
3983 .db 0xa8,0xac,0xac,0xa8,0xac,0xa8,0xa8,0xac
3986 ; ----------------------- Opcode decoding -------------------------
3988 ; Lookup table for Z80 opcodes. Translates the first byte of the instruction word into three
3989 ; operations: fetch, do something, store.
3990 ; The table is made of 256 words. These 16-bit words consist of
3991 ; the fetch operation (bit 0-4), the processing operation (bit 10-16) and the store
3992 ; operation (bit 5-9).
3993 .org (PC+255) & 0xff00
3995 .dw (FETCH_NOP | OP_NOP | STORE_NOP) ; 00 NOP
3996 .dw (FETCH_DIR16| OP_NOP | STORE_BC ) ; 01 nn nn LD BC,nn
3997 .dw (FETCH_A | OP_NOP | STORE_MBC) ; 02 LD (BC),A
3998 .dw (FETCH_BC | OP_INC16 | STORE_BC ) ; 03 INC BC
3999 .dw (FETCH_B | OP_INC | STORE_B ) ; 04 INC B
4000 .dw (FETCH_B | OP_DEC | STORE_B ) ; 05 DEC B
4001 .dw (FETCH_DIR8 | OP_NOP | STORE_B ) ; 06 nn LD B,n
4002 .dw (FETCH_A | OP_RLC | STORE_A ) ; 07 RLCA
4003 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 08 EX AF,AF' (Z80)
4004 .dw (FETCH_BC | OP_ADDHL | STORE_HL ) ; 09 ADD HL,BC
4005 .dw (FETCH_MBC | OP_NOP | STORE_A ) ; 0A LD A,(BC)
4006 .dw (FETCH_BC | OP_DEC16 | STORE_BC ) ; 0B DEC BC
4007 .dw (FETCH_C | OP_INC | STORE_C ) ; 0C INC C
4008 .dw (FETCH_C | OP_DEC | STORE_C ) ; 0D DEC C
4009 .dw (FETCH_DIR8 | OP_NOP | STORE_C ) ; 0E nn LD C,n
4010 .dw (FETCH_A | OP_RRC | STORE_A ) ; 0F RRCA
4011 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 10 oo DJNZ o (Z80)
4012 .dw (FETCH_DIR16| OP_NOP | STORE_DE ) ; 11 nn nn LD DE,nn
4013 .dw (FETCH_A | OP_NOP | STORE_MDE) ; 12 LD (DE),A
4014 .dw (FETCH_DE | OP_INC16 | STORE_DE ) ; 13 INC DE
4015 .dw (FETCH_D | OP_INC | STORE_D ) ; 14 INC D
4016 .dw (FETCH_D | OP_DEC | STORE_D ) ; 15 DEC D
4017 .dw (FETCH_DIR8 | OP_NOP | STORE_D ) ; 16 nn LD D,n
4018 .dw (FETCH_A | OP_RL | STORE_A ) ; 17 RLA
4019 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 18 oo JR o (Z80)
4020 .dw (FETCH_DE | OP_ADDHL | STORE_HL ) ; 19 ADD HL,DE
4021 .dw (FETCH_MDE | OP_NOP | STORE_A ) ; 1A LD A,(DE)
4022 .dw (FETCH_DE | OP_DEC16 | STORE_DE ) ; 1B DEC DE
4023 .dw (FETCH_E | OP_INC | STORE_E ) ; 1C INC E
4024 .dw (FETCH_E | OP_DEC | STORE_E ) ; 1D DEC E
4025 .dw (FETCH_DIR8 | OP_NOP | STORE_E ) ; 1E nn LD E,n
4026 .dw (FETCH_A | OP_RR | STORE_A ) ; 1F RRA
4027 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 20 oo JR NZ,o (Z80)
4028 .dw (FETCH_DIR16| OP_NOP | STORE_HL ) ; 21 nn nn LD HL,nn
4029 .dw (FETCH_DIR16| OP_STHL | STORE_NOP) ; 22 nn nn LD (nn),HL
4030 .dw (FETCH_HL | OP_INC16 | STORE_HL ) ; 23 INC HL
4031 .dw (FETCH_H | OP_INC | STORE_H ) ; 24 INC H
4032 .dw (FETCH_H | OP_DEC | STORE_H ) ; 25 DEC H
4033 .dw (FETCH_DIR8 | OP_NOP | STORE_H ) ; 26 nn LD H,n
4034 .dw (FETCH_A | OP_DA | STORE_A ) ; 27 DAA
4035 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 28 oo JR Z,o (Z80)
4036 .dw (FETCH_HL | OP_ADDHL | STORE_HL ) ; 29 ADD HL,HL
4037 .dw (FETCH_DIR16| OP_RMEM16 | STORE_HL ) ; 2A nn nn LD HL,(nn)
4038 .dw (FETCH_HL | OP_DEC16 | STORE_HL ) ; 2B DEC HL
4039 .dw (FETCH_L | OP_INC | STORE_L ) ; 2C INC L
4040 .dw (FETCH_L | OP_DEC | STORE_L ) ; 2D DEC L
4041 .dw (FETCH_DIR8 | OP_NOP | STORE_L ) ; 2E nn LD L,n
4042 .dw (FETCH_NOP | OP_CPL | STORE_NOP) ; 2F CPL
4043 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 30 oo JR NC,o (Z80)
4044 .dw (FETCH_DIR16| OP_NOP | STORE_SP ) ; 31 nn nn LD SP,nn
4045 .dw (FETCH_DIR16| OP_NOP | STORE_AM ) ; 32 nn nn LD (nn),A
4046 .dw (FETCH_SP | OP_INC16 | STORE_SP ) ; 33 INC SP
4047 .dw (FETCH_MHL | OP_INC | STORE_MHL) ; 34 INC (HL)
4048 .dw (FETCH_MHL | OP_DEC | STORE_MHL) ; 35 DEC (HL)
4049 .dw (FETCH_DIR8 | OP_NOP | STORE_MHL) ; 36 nn LD (HL),n
4050 .dw (FETCH_NOP | OP_SCF | STORE_NOP) ; 37 SCF
4051 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; 38 oo JR C,o (Z80)
4052 .dw (FETCH_SP | OP_ADDHL | STORE_HL ) ; 39 ADD HL,SP
4053 .dw (FETCH_DIR16| OP_RMEM8 | STORE_A ) ; 3A nn nn LD A,(nn)
4054 .dw (FETCH_SP | OP_DEC16 | STORE_SP ) ; 3B DEC SP
4055 .dw (FETCH_NOP | OP_INCA | STORE_NOP) ; 3C INC A
4056 .dw (FETCH_NOP | OP_DECA | STORE_NOP) ; 3D DEC A
4057 .dw (FETCH_DIR8 | OP_NOP | STORE_A ) ; 3E nn LD A,n
4058 .dw (FETCH_NOP | OP_CCF | STORE_NOP) ; 3F CCF (Complement Carry Flag, gvd)
4059 .dw (FETCH_B | OP_NOP | STORE_B ) ; 40 LD B,r
4060 .dw (FETCH_C | OP_NOP | STORE_B ) ; 41 LD B,r
4061 .dw (FETCH_D | OP_NOP | STORE_B ) ; 42 LD B,r
4062 .dw (FETCH_E | OP_NOP | STORE_B ) ; 43 LD B,r
4063 .dw (FETCH_H | OP_NOP | STORE_B ) ; 44 LD B,r
4064 .dw (FETCH_L | OP_NOP | STORE_B ) ; 45 LD B,r
4065 .dw (FETCH_MHL | OP_NOP | STORE_B ) ; 46 LD B,r
4066 .dw (FETCH_A | OP_NOP | STORE_B ) ; 47 LD B,r
4067 .dw (FETCH_B | OP_NOP | STORE_C ) ; 48 LD C,r
4068 .dw (FETCH_C | OP_NOP | STORE_C ) ; 49 LD C,r
4069 .dw (FETCH_D | OP_NOP | STORE_C ) ; 4A LD C,r
4070 .dw (FETCH_E | OP_NOP | STORE_C ) ; 4B LD C,r
4071 .dw (FETCH_H | OP_NOP | STORE_C ) ; 4C LD C,r
4072 .dw (FETCH_L | OP_NOP | STORE_C ) ; 4D LD C,r
4073 .dw (FETCH_MHL | OP_NOP | STORE_C ) ; 4E LD C,r
4074 .dw (FETCH_A | OP_NOP | STORE_C ) ; 4F LD C,r
4075 .dw (FETCH_B | OP_NOP | STORE_D ) ; 50 LD D,r
4076 .dw (FETCH_C | OP_NOP | STORE_D ) ; 51 LD D,r
4077 .dw (FETCH_D | OP_NOP | STORE_D ) ; 52 LD D,r
4078 .dw (FETCH_E | OP_NOP | STORE_D ) ; 53 LD D,r
4079 .dw (FETCH_H | OP_NOP | STORE_D ) ; 54 LD D,r
4080 .dw (FETCH_L | OP_NOP | STORE_D ) ; 55 LD D,r
4081 .dw (FETCH_MHL | OP_NOP | STORE_D ) ; 56 LD D,r
4082 .dw (FETCH_A | OP_NOP | STORE_D ) ; 57 LD D,r
4083 .dw (FETCH_B | OP_NOP | STORE_E ) ; 58 LD E,r
4084 .dw (FETCH_C | OP_NOP | STORE_E ) ; 59 LD E,r
4085 .dw (FETCH_D | OP_NOP | STORE_E ) ; 5A LD E,r
4086 .dw (FETCH_E | OP_NOP | STORE_E ) ; 5B LD E,r
4087 .dw (FETCH_H | OP_NOP | STORE_E ) ; 5C LD E,r
4088 .dw (FETCH_L | OP_NOP | STORE_E ) ; 5D LD E,r
4089 .dw (FETCH_MHL | OP_NOP | STORE_E ) ; 5E LD E,r
4090 .dw (FETCH_A | OP_NOP | STORE_E ) ; 5F LD E,r
4091 .dw (FETCH_B | OP_NOP | STORE_H ) ; 60 LD H,r
4092 .dw (FETCH_C | OP_NOP | STORE_H ) ; 61 LD H,r
4093 .dw (FETCH_D | OP_NOP | STORE_H ) ; 62 LD H,r
4094 .dw (FETCH_E | OP_NOP | STORE_H ) ; 63 LD H,r
4095 .dw (FETCH_H | OP_NOP | STORE_H ) ; 64 LD H,r
4096 .dw (FETCH_L | OP_NOP | STORE_H ) ; 65 LD H,r
4097 .dw (FETCH_MHL | OP_NOP | STORE_H ) ; 66 LD H,r
4098 .dw (FETCH_A | OP_NOP | STORE_H ) ; 67 LD H,r
4099 .dw (FETCH_B | OP_NOP | STORE_L ) ; 68 LD L,r
4100 .dw (FETCH_C | OP_NOP | STORE_L ) ; 69 LD L,r
4101 .dw (FETCH_D | OP_NOP | STORE_L ) ; 6A LD L,r
4102 .dw (FETCH_E | OP_NOP | STORE_L ) ; 6B LD L,r
4103 .dw (FETCH_H | OP_NOP | STORE_L ) ; 6C LD L,r
4104 .dw (FETCH_L | OP_NOP | STORE_L ) ; 6D LD L,r
4105 .dw (FETCH_MHL | OP_NOP | STORE_L ) ; 6E LD L,r
4106 .dw (FETCH_A | OP_NOP | STORE_L ) ; 6F LD L,r
4107 .dw (FETCH_B | OP_NOP | STORE_MHL) ; 70 LD (HL),r
4108 .dw (FETCH_C | OP_NOP | STORE_MHL) ; 71 LD (HL),r
4109 .dw (FETCH_D | OP_NOP | STORE_MHL) ; 72 LD (HL),r
4110 .dw (FETCH_E | OP_NOP | STORE_MHL) ; 73 LD (HL),r
4111 .dw (FETCH_H | OP_NOP | STORE_MHL) ; 74 LD (HL),r
4112 .dw (FETCH_L | OP_NOP | STORE_MHL) ; 75 LD (HL),r
4113 .dw (FETCH_NOP | OP_NOP | STORE_NOP) ; 76 HALT
4114 .dw (FETCH_A | OP_NOP | STORE_MHL) ; 77 LD (HL),r
4115 .dw (FETCH_B | OP_NOP | STORE_A ) ; 78 LD A,r
4116 .dw (FETCH_C | OP_NOP | STORE_A ) ; 79 LD A,r
4117 .dw (FETCH_D | OP_NOP | STORE_A ) ; 7A LD A,r
4118 .dw (FETCH_E | OP_NOP | STORE_A ) ; 7B LD A,r
4119 .dw (FETCH_H | OP_NOP | STORE_A ) ; 7C LD A,r
4120 .dw (FETCH_L | OP_NOP | STORE_A ) ; 7D LD A,r
4121 .dw (FETCH_MHL | OP_NOP | STORE_A ) ; 7E LD A,r
4122 .dw (FETCH_A | OP_NOP | STORE_A ) ; 7F LD A,r
4123 .dw (FETCH_B | OP_ADDA | STORE_NOP) ; 80 ADD A,r
4124 .dw (FETCH_C | OP_ADDA | STORE_NOP) ; 81 ADD A,r
4125 .dw (FETCH_D | OP_ADDA | STORE_NOP) ; 82 ADD A,r
4126 .dw (FETCH_E | OP_ADDA | STORE_NOP) ; 83 ADD A,r
4127 .dw (FETCH_H | OP_ADDA | STORE_NOP) ; 84 ADD A,r
4128 .dw (FETCH_L | OP_ADDA | STORE_NOP) ; 85 ADD A,r
4129 .dw (FETCH_MHL | OP_ADDA | STORE_NOP) ; 86 ADD A,r
4130 .dw (FETCH_A | OP_ADDA | STORE_NOP) ; 87 ADD A,r
4131 .dw (FETCH_B | OP_ADCA | STORE_NOP) ; 88 ADC A,r
4132 .dw (FETCH_C | OP_ADCA | STORE_NOP) ; 89 ADC A,r
4133 .dw (FETCH_D | OP_ADCA | STORE_NOP) ; 8A ADC A,r
4134 .dw (FETCH_E | OP_ADCA | STORE_NOP) ; 8B ADC A,r
4135 .dw (FETCH_H | OP_ADCA | STORE_NOP) ; 8C ADC A,r
4136 .dw (FETCH_L | OP_ADCA | STORE_NOP) ; 8D ADC A,r
4137 .dw (FETCH_MHL | OP_ADCA | STORE_NOP) ; 8E ADC A,r
4138 .dw (FETCH_A | OP_ADCA | STORE_NOP) ; 8F ADC A,r
4139 .dw (FETCH_B | OP_SUBFA | STORE_NOP) ; 90 SUB A,r
4140 .dw (FETCH_C | OP_SUBFA | STORE_NOP) ; 91 SUB A,r
4141 .dw (FETCH_D | OP_SUBFA | STORE_NOP) ; 92 SUB A,r
4142 .dw (FETCH_E | OP_SUBFA | STORE_NOP) ; 93 SUB A,r
4143 .dw (FETCH_H | OP_SUBFA | STORE_NOP) ; 94 SUB A,r
4144 .dw (FETCH_L | OP_SUBFA | STORE_NOP) ; 95 SUB A,r
4145 .dw (FETCH_MHL | OP_SUBFA | STORE_NOP) ; 96 SUB A,r
4146 .dw (FETCH_A | OP_SUBFA | STORE_NOP) ; 97 SUB A,r
4147 .dw (FETCH_B | OP_SBCFA | STORE_NOP) ; 98 SBC A,r
4148 .dw (FETCH_C | OP_SBCFA | STORE_NOP) ; 99 SBC A,r
4149 .dw (FETCH_D | OP_SBCFA | STORE_NOP) ; 9A SBC A,r
4150 .dw (FETCH_E | OP_SBCFA | STORE_NOP) ; 9B SBC A,r
4151 .dw (FETCH_H | OP_SBCFA | STORE_NOP) ; 9C SBC A,r
4152 .dw (FETCH_L | OP_SBCFA | STORE_NOP) ; 9D SBC A,r
4153 .dw (FETCH_MHL | OP_SBCFA | STORE_NOP) ; 9E SBC A,r
4154 .dw (FETCH_A | OP_SBCFA | STORE_NOP) ; 9F SBC A,r
4155 .dw (FETCH_B | OP_ANDA | STORE_NOP) ; A0 AND A,r
4156 .dw (FETCH_C | OP_ANDA | STORE_NOP) ; A1 AND A,r
4157 .dw (FETCH_D | OP_ANDA | STORE_NOP) ; A2 AND A,r
4158 .dw (FETCH_E | OP_ANDA | STORE_NOP) ; A3 AND A,r
4159 .dw (FETCH_H | OP_ANDA | STORE_NOP) ; A4 AND A,r
4160 .dw (FETCH_L | OP_ANDA | STORE_NOP) ; A5 AND A,r
4161 .dw (FETCH_MHL | OP_ANDA | STORE_NOP) ; A6 AND A,r
4162 .dw (FETCH_A | OP_ANDA | STORE_NOP) ; A7 AND A,r
4163 .dw (FETCH_B | OP_XORA | STORE_NOP) ; A8 XOR A,r
4164 .dw (FETCH_C | OP_XORA | STORE_NOP) ; A9 XOR A,r
4165 .dw (FETCH_D | OP_XORA | STORE_NOP) ; AA XOR A,r
4166 .dw (FETCH_E | OP_XORA | STORE_NOP) ; AB XOR A,r
4167 .dw (FETCH_H | OP_XORA | STORE_NOP) ; AC XOR A,r
4168 .dw (FETCH_L | OP_XORA | STORE_NOP) ; AD XOR A,r
4169 .dw (FETCH_MHL | OP_XORA | STORE_NOP) ; AE XOR A,r
4170 .dw (FETCH_A | OP_XORA | STORE_NOP) ; AF XOR A,r
4171 .dw (FETCH_B | OP_ORA | STORE_NOP) ; B0 OR A,r
4172 .dw (FETCH_C | OP_ORA | STORE_NOP) ; B1 OR A,r
4173 .dw (FETCH_D | OP_ORA | STORE_NOP) ; B2 OR A,r
4174 .dw (FETCH_E | OP_ORA | STORE_NOP) ; B3 OR A,r
4175 .dw (FETCH_H | OP_ORA | STORE_NOP) ; B4 OR A,r
4176 .dw (FETCH_L | OP_ORA | STORE_NOP) ; B5 OR A,r
4177 .dw (FETCH_MHL | OP_ORA | STORE_NOP) ; B6 OR A,r
4178 .dw (FETCH_A | OP_ORA | STORE_NOP) ; B7 OR A,r
4179 .dw (FETCH_B | OP_CPFA | STORE_NOP) ; B8 CP A,r
4180 .dw (FETCH_C | OP_CPFA | STORE_NOP) ; B9 CP A,r
4181 .dw (FETCH_D | OP_CPFA | STORE_NOP) ; BA CP A,r
4182 .dw (FETCH_E | OP_CPFA | STORE_NOP) ; BB CP A,r
4183 .dw (FETCH_H | OP_CPFA | STORE_NOP) ; BC CP A,r
4184 .dw (FETCH_L | OP_CPFA | STORE_NOP) ; BD CP A,r
4185 .dw (FETCH_MHL | OP_CPFA | STORE_NOP) ; BE CP A,r
4186 .dw (FETCH_A | OP_CPFA | STORE_NOP) ; BF CP A,r
4187 .dw (FETCH_NOP | OP_IFNZ | STORE_RET) ; C0 RET NZ
4188 .dw (FETCH_NOP | OP_POP16 | STORE_BC ) ; C1 POP BC
4189 .dw (FETCH_DIR16| OP_IFNZ | STORE_PC ) ; C2 nn nn JP NZ,nn
4190 .dw (FETCH_DIR16| OP_NOP | STORE_PC ) ; C3 nn nn JP nn
4191 .dw (FETCH_DIR16| OP_IFNZ | STORE_CALL) ; C4 nn nn CALL NZ,nn
4192 .dw (FETCH_BC | OP_PUSH16 | STORE_NOP) ; C5 PUSH BC
4193 .dw (FETCH_DIR8 | OP_ADDA | STORE_NOP) ; C6 nn ADD A,n
4194 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; C7 RST 0
4195 .dw (FETCH_NOP | OP_IFZ | STORE_RET) ; C8 RET Z
4196 .dw (FETCH_NOP | OP_NOP | STORE_RET) ; C9 RET
4197 .dw (FETCH_DIR16| OP_IFZ | STORE_PC ) ; CA nn nn JP Z,nn
4198 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; CB (Z80 specific)
4199 .dw (FETCH_DIR16| OP_IFZ | STORE_CALL) ; CC nn nn CALL Z,nn
4200 .dw (FETCH_DIR16| OP_NOP | STORE_CALL) ; CD nn nn CALL nn
4201 .dw (FETCH_DIR8 | OP_ADCA | STORE_NOP) ; CE nn ADC A,n
4202 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; CF RST 8H
4203 .dw (FETCH_NOP | OP_IFNC | STORE_RET) ; D0 RET NC
4204 .dw (FETCH_NOP | OP_POP16 | STORE_DE ) ; D1 POP DE
4205 .dw (FETCH_DIR16| OP_IFNC | STORE_PC ) ; D2 nn nn JP NC,nn
4206 .dw (FETCH_DIR8 | OP_OUTA | STORE_NOP) ; D3 nn OUT (n),A
4207 .dw (FETCH_DIR16| OP_IFNC | STORE_CALL) ; D4 nn nn CALL NC,nn
4208 .dw (FETCH_DE | OP_PUSH16 | STORE_NOP) ; D5 PUSH DE
4209 .dw (FETCH_DIR8 | OP_SUBFA | STORE_NOP) ; D6 nn SUB n
4210 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; D7 RST 10H
4211 .dw (FETCH_NOP | OP_IFC | STORE_RET) ; D8 RET C
4212 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; D9 EXX (Z80)
4213 .dw (FETCH_DIR16| OP_IFC | STORE_PC ) ; DA nn nn JP C,nn
4214 .dw (FETCH_DIR8 | OP_IN | STORE_A ) ; DB nn IN A,(n)
4215 .dw (FETCH_DIR16| OP_IFC | STORE_CALL) ; DC nn nn CALL C,nn
4216 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; DD (Z80)
4217 .dw (FETCH_DIR8 | OP_SBCFA | STORE_NOP) ; DE nn SBC A,n
4218 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; DF RST 18H
4219 .dw (FETCH_NOP | OP_IFPO | STORE_RET) ; E0 RET PO
4220 .dw (FETCH_NOP | OP_POP16 | STORE_HL ) ; E1 POP HL
4221 .dw (FETCH_DIR16| OP_IFPO | STORE_PC ) ; E2 nn nn JP PO,nn
4222 .dw (FETCH_MSP | OP_EXHL | STORE_MSP) ; E3 EX (SP),HL
4223 .dw (FETCH_DIR16| OP_IFPO | STORE_CALL) ; E4 nn nn CALL PO,nn
4224 .dw (FETCH_HL | OP_PUSH16 | STORE_NOP) ; E5 PUSH HL
4225 .dw (FETCH_DIR8 | OP_ANDA | STORE_NOP) ; E6 nn AND n
4226 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; E7 RST 20H
4227 .dw (FETCH_NOP | OP_IFPE | STORE_RET) ; E8 RET PE
4228 .dw (FETCH_HL | OP_NOP | STORE_PC ) ; E9 JP (HL)
4229 .dw (FETCH_DIR16| OP_IFPE | STORE_PC ) ; EA nn nn JP PE,nn
4230 .dw (FETCH_DE | OP_EXHL | STORE_DE ) ; EB EX DE,HL
4231 .dw (FETCH_DIR16| OP_IFPE | STORE_CALL) ; EC nn nn CALL PE,nn
4232 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; ED (Z80 specific)
4233 .dw (FETCH_DIR8 | OP_XORA | STORE_NOP) ; EE nn XOR n
4234 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; EF RST 28H
4235 .dw (FETCH_NOP | OP_IFP | STORE_RET) ; F0 RET P
4236 .dw (FETCH_NOP | OP_POP16 | STORE_AF ) ; F1 POP AF
4237 .dw (FETCH_DIR16| OP_IFP | STORE_PC ) ; F2 nn nn JP P,nn
4238 .dw (FETCH_NOP | OP_DI | STORE_NOP) ; F3 DI
4239 .dw (FETCH_DIR16| OP_IFP | STORE_CALL) ; F4 nn nn CALL P,nn
4240 .dw (FETCH_AF | OP_PUSH16 | STORE_NOP) ; F5 PUSH AF
4241 .dw (FETCH_DIR8 | OP_ORA | STORE_NOP) ; F6 nn OR n
4242 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; F7 RST 30H
4243 .dw (FETCH_NOP | OP_IFM | STORE_RET) ; F8 RET M
4244 .dw (FETCH_HL | OP_NOP | STORE_SP ) ; F9 LD SP,HL
4245 .dw (FETCH_DIR16| OP_IFM | STORE_PC ) ; FA nn nn JP M,nn
4246 .dw (FETCH_NOP | OP_EI | STORE_NOP) ; FB EI
4247 .dw (FETCH_DIR16| OP_IFM | STORE_CALL) ; FC nn nn CALL M,nn
4248 .dw (FETCH_NOP | OP_INV | STORE_NOP) ; FD (Z80 specific)
4249 .dw (FETCH_DIR8 | OP_CPFA | STORE_NOP) ; FE nn CP n
4250 .dw (FETCH_RST | OP_NOP | STORE_CALL) ; FF RST 38H
4252 ; vim:set ts=8 noet nowrap