Simulating an 8080. Is there a better way to do this?
Posted: Thu Mar 25, 2021 8:39 am
I am working on simulating the instructions of an 8080 processor.
The following code fragments simulate the dcr A and dcr M instructions. Unlike other processors, the 8080 increment and decrement instructions affect the aux carry flag.
Is there a better way to do this?
The only thing I can think of is storing the values and not calculating the condition codes until/unless they are needed.
The following code fragments simulate the dcr A and dcr M instructions. Unlike other processors, the 8080 increment and decrement instructions affect the aux carry flag.
Is there a better way to do this?
Code: Select all
02157 ;
02158 ; dcr A ; Decrement register A
02159 ;
AFCA 02160 dcrA_:
AFCA A6 15 [3] 02161 ldx Reg_A ; Decrement the register
AFCC CA [2] 02162 dex
02163
AFCD 8A [2] 02164 txa ; Make a copy of the result
AFCE 45 15 [3] 02165 eor Reg_A ; Borrowed from upper nybble if its low bit changed
02166
AFD0 86 15 [3] 02167 stx Reg_A ; Store the new value
02168
AFD2 02169 dcrCommon:
AFD2 29 10 [2] 02170 and #$10 ; Isolate and save aux carry flag
AFD4 85 2B [3] 02171 sta Scratch
02172
AFD6 02173 dcrMCommon:
AFD6 A5 14 [3] 02174 lda Reg_CC ; Clear the flags we are updating
AFD8 29 2A [2] 02175 and #$2A
02176
AFDA 05 2B [3] 02177 ora Scratch ; Fold in the aux carry flag
02178
AFDC 1D AA90 [4/5] 02179 ora ValueFlags,X ; Set the S, Z and P flags accordingly
AFDF 85 14 [3] 02180 sta Reg_CC
02181
AFE1 4C ADE4 [3] 02182 jmp PCPlus1
Code: Select all
02240 ;
02241 ; dcr M ; Decrement memory register
02242 ;
B010 02243 dcrM_:
B010 18 [2] 02244 clc
B011 A5 1A [3] 02245 lda Reg_L ; Get target address
B013 69 00 [2] 02246 adc #<RAM80
B015 85 12 [3] 02247 sta Ptr
B017 A5 1B [3] 02248 lda Reg_H
B019 69 0B [2] 02249 adc #>RAM80
B01B 85 13 [3] 02250 sta Ptr+1
02251
B01D A0 00 [2] 02252 ldy #0
B01F B1 12 [5/6] 02253 lda (Ptr),Y
B021 AA [2] 02254 tax ; Decrement the register
B022 CA [2] 02255 dex
02256
B023 8A [2] 02257 txa ; Make a copy of the result
B024 51 12 [5/6] 02258 eor (Ptr),Y ; Borrowed from upper nybble if its low bit changed
B026 29 10 [2] 02259 and #$10 ; Isolate and save aux carry flag
B028 85 2B [3] 02260 sta Scratch
02261
B02A 8A [2] 02262 txa ; Store the new value
B02B 91 12 [6] 02263 sta (Ptr),Y
02264
B02D 4C AFD6 [3] 02265 jmp dcrMCommon