Rewrite macros to be more flexible

This commit is contained in:
hugova 2025-03-30 16:15:07 +02:00
parent abcac12471
commit 1144fd01ca

280
wip-hugo/macros/16aritmatic.s Normal file → Executable file
View file

@ -1,140 +1,140 @@
;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;; A file containing 16-bit macro arithmatic. ;; A file containing 16-bit macro arithmatic.
;; You may add ,! ass a 5:th parameter to skipp flagg clearing. ;; You may add ,! ass a 5:th parameter to skipp flagg clearing.
;; This will make it run faster but will have unintended behavior. ;; This will make it run faster but will have unintended behavior.
;;Se below for some fast 16bit logic ;;Se below for some fast 16bit logic
;;http://6502.org/tutorials/compare_beyond.html ;;http://6502.org/tutorials/compare_beyond.html
;; Addition uses the A register ;; Addition always uses the A register
;; a = a + b ;; a = a + b
.macro Add_16 a_low, a_hi, b_low, b_hi, fast_unsafe .macro Add_16 a_low, a_hi, b_low, b_hi, fast_unsafe
;; IF to run it fast ;; IF to run it fast
.ifblank fast_unsafe .ifblank fast_unsafe
CLC CLC
.endif .endif
LDA b_low .if .not .match ({b_low}, a)
ADC a_low LDA b_low
STA a_low .endif ;;untested
LDA b_hi ADC a_low
ADC a_hi STA a_low
STA a_hi .if .match ({b_hi}, x) ;;Untested
.endmacro TXA
.else
;; Untested! LDA b_hi
;; Addition uses the A register .endif
;; a = a + b. b_low = A, b_hi = X ADC a_hi
.macro Add_16_AX low, hi, fast_unsafe STA a_hi
;; IF to run it fast .endmacro
.ifblank fast_unsafe
CLC
.endif ;; Subtraction uses always uses A register
ADC low ;; a = a - b
STA low .macro Sub_16 a_low, a_hi, b_low, b_hi, fast_unsafe
TXA ;; IF to run it fast
ADC hi .ifblank fast_unsafe
STA hi SEC
.endmacro .endif
;; if b_low = A and b_hi =A
.macro Add_16_A a_low, a_hi, b_low, b_hi, fast_unsafe .if .match({b_low}, a) .and .match({b_hi}, a) ;;untested
;; IF to run it fast .LOCAL rewrite
.ifblank fast_unsafe .LOCAL rewrite2
CLC STA rewrite +1
.endif STA rewrite2 + 1
LDA a_low LDA a_low
ADC b_low rewrite:
STA a_low SBC #b_low
LDA b_hi STA a_low
ADC a_hi LDA a_hi
STA a_hi rewrite2:
.endmacro SBC #b_hi
STA a_hi
;; Subtraction uses the A register ;; if b_low = A
;; a = a - b .elseif .match({b_low}, a) ;;untested
.macro Sub_16 a_low, a_hi, b_low, b_hi, fast_unsafe .LOCAL rewrite
;; IF to run it fast STA rewrite +1
.ifblank fast_unsafe LDA a_low
SEC rewrite:
.endif SBC #b_low
LDA a_low STA a_low
SBC b_low LDA a_hi
STA a_low SBC b_hi
LDA a_hi STA a_hi
SBC b_hi .elseif .match({b_hi}, a) ;;untested
STA a_hi .LOCAL rewrite
.endmacro STA rewrite +1
LDA a_low
.macro Sub_16_A a_low, a_hi, b_low, b_hi, fast_unsafe SBC b_low
;; IF to run it fast STA a_low
.ifblank fast_unsafe LDA a_hi
SEC rewrite:
.endif SBC #b_hi
SBC b_low STA a_hi
STA a_low .else
LDA a_hi LDA a_low
SBC b_hi SBC b_low
STA a_hi STA a_low
.endmacro LDA a_hi
SBC b_hi
;; Untested STA a_hi
;; Subtraction uses the A register .endif
;; a = a - b. b_low = A, b_hi = X .endmacro
.macro Sub_16_AX low, hi, fast_unsafe
;; IF to run it fast ;; Subtraction uses always uses A register
.ifblank fast_unsafe ;; a = (A, a_hi) - b
SEC .macro Sub_16_A a_low, a_hi, b_low, b_hi, fast_unsafe
.endif ;; IF to run it fast
SBC low .ifblank fast_unsafe
STA low SEC
TXA .endif
SBC hi SBC b_low
STA hi STA a_low
.endmacro LDA a_hi
SBC b_hi
;; Multiplication of 2 STA a_hi
;; a = a*2 .endmacro
.macro Mult_16 low_, hi_, NOT_ROL
;; IF NOT_ROL
.ifblank fast_unsafe ;; Multiplication of 2
ASL low_ ;; a = a*2
ROL hi_ .macro Mult_16 low_, hi_, NOT_ROL
.else ;; IF NOT_ROL
ROL low_ .ifblank fast_unsafe
ROL hi_ ASL low_
.endif ROL hi_
.endmacro .else
ROL low_
.macro Mov_16 a_low, a_hi, b_low, b_hi ROL hi_
LDA b_low .endif
STA a_low .endmacro
LDA b_hi
STA a_hi .macro Mov_16 a_low, a_hi, b_low, b_hi
.endmacro LDA b_low
STA a_low
;;Larger then operation, uses the A register LDA b_hi
;;IF a < b then: jump to label STA a_hi
; C =0 if jump to LABEL .endmacro
.macro Lag_16 a_low, a_hi, b_low, b_hi, label
LDA a_hi ;;http://www.6502.org/tutorials/compare_beyond.html
CMP b_hi ;;Larger then operation, uses the A register
BCC label ;;IF a < b then: jump to label
BNE LABEL ; C =0 if jump to LABEL
LDA a_low .macro Lag_16 a_low, a_hi, b_low, b_hi, label
CMP b_low .LOCAL LABEL
BCC label LDA a_hi
LABEL: CMP b_hi
.endmacro BCC label
BNE LABEL
.macro Lag_16y a_low, a_hi, b_hi, label LDA a_low
LDA a_hi .if .match ({b_low}, a)
CMP b_hi .LOCAL next
BCC label STY next +1
BNE LABEL next:
LDA a_low CMP #$ff
STY next +1 .else
next: CMP b_low
CMP #$ff .endif
BCC label BCC label
LABEL: LABEL:
.endmacro .endmacro