changeset 7:c7e6b6318fea

Commented the line drawing routine along with some other minor bits Most notably, identified and described the routine that draws lines for the line art in the dungeon. As a result, identified V22D through V249. Various other relatively minor comments added or updated.
author William Astle <lost@l-w.ca>
date Sun, 26 May 2013 01:10:11 -0600
parents 2011ab76f008
children 7599c6d44eb5
files dod.s
diffstat 1 files changed, 276 insertions(+), 253 deletions(-) [+]
line wrap: on
line diff
--- a/dod.s	Sat May 25 22:04:50 2013 -0600
+++ b/dod.s	Sun May 26 01:10:11 2013 -0600
@@ -214,27 +214,22 @@
 V229		rmb 2
 V22B		rmb 1
 V22C		rmb 1				the current level background colour mask
-V22D		rmb 1
-V22E		rmb 1
-V22F		rmb 2
-V231		rmb 2
-V233		rmb 2
-V235		rmb 2
-V237		rmb 1
-V238		rmb 1
-V239		rmb 1
-V23A		rmb 1
-V23B		rmb 1
-V23C		rmb 1
-V23D		rmb 1
-V23E		rmb 2
-V240		rmb 1
-V241		rmb 2
-V243		rmb 2
-V245		rmb 1
-V246		rmb 1
-V247		rmb 2
-V249		rmb 6
+V22D		rmb 1				the current light level, $ff means dark
+V22E		rmb 1				counter between pixels when drawing lines
+V22F		rmb 2				start Y coord for line drawing
+V231		rmb 2				start X coord for line drawing
+V233		rmb 2				end Y coord for line drawing
+V235		rmb 2				end X coord for line drawing
+V237		rmb 3				current X coordinate when drawing line
+V23A		rmb 3				current Y coordinate when drawing line
+V23D		rmb 3				difference in X coordinate between pixels when drawing line
+V240		rmb 3				difference in Y coordinate between pixels when drawing line
+V243		rmb 2				number of pixels to draw in a line
+V245		rmb 1				the offset to add to pointer when moving to new byte (line drawing)
+V246		rmb 1				the offset to add to pointer when moving to new row (line drawing)
+V247		rmb 2				start address of drawing area (line drawing)
+V249		rmb 2				end address of drawing area (line drawing)
+V24B		rmb 4
 V24F		rmb 1
 V250		rmb 1
 V251		rmb 1
@@ -305,9 +300,9 @@
 V2BC		rmb 1				keyboard buffer read offset
 V2BD		rmb 1				keyboard buffer write offset
 		rmb 3
-V2C1		rmb 1
-V2C2		rmb 1
-V2C3		rmb 1
+V2C1		rmb 1				temporary variable used during line drawing and division
+V2C2		rmb 1				temporary variable used during division
+V2C3		rmb 1				temporary variable used during division
 V2C4		rmb 1
 V2C5		rmb 1
 V2C6		rmb 1
@@ -430,9 +425,9 @@
 		inca
 		cmpu #LDAA3
 		blo LC08C
-		ldu #V388
+		ldu #V388				point to text parameters for status area
 		dec V2B7				indicate nonstandard text area
-		clearstatus
+		clearstatus				blank status line (where we'll put the copyright notice)
 		renderstrimmp				display copyright message
 		fcb $f8,$df,$0c,$c9			packed string "COPYRIGHT  DYNA MICRO  MCMLXXXII"
 		fcb $27,$45,$00,$02
@@ -447,33 +442,33 @@
 		ldx #LDF10
 		dec V29E
 		dod S14
-		renderstrimmp			display "I DARE YE ENTER..."
+		renderstrimmp				display <CR>"I DARE YE ENTER..."<CR>
 		fcb $9f,$d2,$02,$06
 		fcb $45,$06,$4a,$02
 		fcb $ba,$85,$97,$bd
 		fcb $ef,$80
-		renderstrimmp			display "THE DUNGEONS OF DAGGORATH!!!!"
+		renderstrimmp				display "...THE DUNGEONS OF DAGGORATH!!!"
 		fcb $f7,$bd,$ea,$20
 		fcb $a0,$25,$5c,$72
 		fcb $bd,$d3,$03,$cc
 		fcb $02,$04,$e7,$7c
 		fcb $83,$44,$6f,$7b
-		delay
-		delay
+		delay					; wait for about 2.6 seconds
+		delay					;
 		dod S15
-		cleargfx2
+		cleargfx2				clear second graphics screen
 		dec V2B4
 		sync
 		lda #2
 		ldu #LD7D5
 		bra LC131
-LC114		ldd #$343c
-		sta PIA1+1
-		stb PIA1+3
-		inca
-		sta PIA0+3
-		cwai #$ef
-		rts
+LC114		ldd #$343c				set up initializers for PIAs
+		sta PIA1+1				set data mode, no interrupts, cassette off
+		stb PIA1+3				set data mode, no interrupts, sound on
+		inca					adjust to enable interrupt
+		sta PIA0+3				set data mode, enable VSYNC, clear MSB of analog MUX
+		cwai #$ef				enable IRQ and wait for one
+		rts					return to caller
 LC124		bsr LC114
 		ldd #$100b
 		std V213
@@ -779,52 +774,53 @@
 LC39F		stb V22D
 		rts
 ; SWI 1 routine
+; Render a graphic from (X)
 LC3A2		clr V251
 		lda V22D
 		inca
 		beq LC3F6
-LC3A9		ldb ,x
-		subb #$fa
-		blo LC3CF
-		leax 1,x
-		ldy #LC3B9
-		ldb b,y
-		jmp b,y
-LC3B9		fcb LC3C9-LC3B9
-		fcb LC3BF-LC3B9
-		fcb LC417-LC3B9
-		fcb LC3C6-LC3B9
-		fcb LC3F6-LC3B9
-		fcb LC3CB-LC3B9
-LC3BF		ldd ,x++
-		stx ,--s
-		tfr d,x
-		skip2
-LC3C6		ldx ,x
-		skip2
-LC3C9		ldx ,s++
+LC3A9		ldb ,x				fetch input data
+		subb #$fa			adjust for operation codes
+		blo LC3CF			brif not operation code
+		leax 1,x			move on to next image data byte
+		ldy #LC3B9			point to jump table for operation codes
+		ldb b,y				get offset to operation routine
+		jmp b,y				execute operation routine
+LC3B9		fcb LC3C9-LC3B9			(FA) return from a "subroutine"
+		fcb LC3BF-LC3B9			(FB) call a "subroutine"
+		fcb LC417-LC3B9			(FC) polygon
+		fcb LC3C6-LC3B9			(FD) jump to a new "routine"
+		fcb LC3F6-LC3B9			(FE) end of input - return to caller
+		fcb LC3CB-LC3B9			(FF)
+LC3BF		ldd ,x++			get address of "subroutine" to call
+		stx ,--s			save return address
+		tfr d,x				set new "execution" address
+		skip2				skip next instruction
+LC3C6		ldx ,x				get address of "routine" to jump to
+		skip2				skip next instruction
+LC3C9		ldx ,s++			get back saved input location
 LC3CB		clr V251
-		bra LC3A9
+		bra LC3A9			go process more input
 LC3CF		tst V251
 		bne LC3D9
 		bsr LC3E2
 		dec V251
-		bra LC3A9
+		bra LC3A9			go process more input
 LC3D9		bsr LC3E0
 		jsr LCAB7
 		bra LC3A9
 LC3E0		bsr LC3F7
-LC3E2		ldb ,x+
-		stb V254
+LC3E2		ldb ,x+				get the input byte and move pointer on
+		stb V254			save input byte for later
 		bsr LC400
 		addd V207
 		std V233
-		ldb ,x+
-		stb V252
+		ldb ,x+				get the next input byte and move pointer on
+		stb V252			save it for later
 		bsr LC406
 		addd V205
 		std V235
-LC3F6		rts
+LC3F6		rts				return to caller
 LC3F7		ldd V233
 		std V22F
 		ldd V235
@@ -840,48 +836,48 @@
 		bra LC414
 LC40F		negb
 		mul
-		jsr LCA99
-LC414		jmp LD377
-LC417		lda ,x+
-		beq LC3CB
+		jsr LCA99			negate D
+LC414		jmp LD377			shift right 7 bits (signed) and return
+LC417		lda ,x+				get next byte in input
+		beq LC3CB			brif NUL - end of values
 		bsr LC3F7
-		ldb -1,x
-		asrb
-		asrb
-		asrb
-		asrb
-		lslb
-		addb V254
-		stb V254
+		ldb -1,x			get the byte following operation code
+		asrb				; move high nibble to low nibble, keep sign
+		asrb				;
+		asrb				;
+		asrb				;
+		lslb				and multiply by two
+		addb V254			add in previous coordinate
+		stb V254			save new coordinate
 		bsr LC400
 		addd V207
 		std V233
-		ldb -1,x
-		andb #$0f
-		bitb #8
-		beq LC438
-		orb #$f0
-LC438		lslb
-		addb V252
-		stb V252
+		ldb -1,x			get back the input byte again
+		andb #$0f			mask off the upper bits
+		bitb #8				is bit 3 set?
+		beq LC438			brif not
+		orb #$f0			sign extend to 8 bits
+LC438		lslb				multiply by two
+		addb V252			add in saved coordinate
+		stb V252			save new coordinate
 		bsr LC406
 		addd V205
 		std V235
-		jsr LCAB7
-		bra LC417
+		jsr LCAB7			go draw a line
+		bra LC417			look for another line segment
 ; swi 2 routine
 ; fetch a packed string immediately following the call and display it
 LC448		ldx 12,s			fetch return address - string address
 		decodestrsb			go decode string
 		stx 12,s			save new return address - after string
 		ldx #V335			point to decoded string
-		skip2
-LC452		renderchar				display character in A
+		skip2				skip the next instruction - nothing to display yet
+LC452		renderchar			display character in A
 ; swi 3 routine
 ; display an unpacked string pointed to by X
 LC454		lda ,x+				fetch byte from string
 		bpl LC452			brif not end of string - display it
-		rts
+		rts				return to caller
 ; swi 4 routine
 ; display character in A
 LC459		tst V2B7			are we looking for standard text mode?
@@ -911,7 +907,7 @@
 		beq LC489			brif so
 		leax 1,x			move pointer forward
 LC489		stx 6,s				save pointer past end of input
-		rts
+		rts				return to caller
 LC48C		pshs a,u			save registers
 		lda ,y				get value counter
 		ldu #LC4A2			point to value handlers
@@ -936,7 +932,7 @@
 		lsrb				; align in low bits of B
 LC4AD		lsrb				;
 LC4AE		lsrb				;
-		rts
+		rts				return to caller
 ; value 1: lower 3 bits of current input byte and upper 2 bits of next one
 ; consumes a byte
 LC4B0		ldd ,x+				fetch input data and consume a byte
@@ -962,7 +958,7 @@
 ; value 7: low 5 bits of current input byte
 ; consumes a byte
 LC4CC		ldb ,x+				fetch input data - already aligned
-		rts
+		rts				return to caller
 ; swi 7 routine
 LC4CF		ldx #8
 LC4D2		clrb
@@ -1084,7 +1080,8 @@
 		cmpx V221
 		blo LC5B5
 		rts
-LC5B5		ldx #LDF10
+; This routine handles player death
+LC5B5		ldx #LDF10			
 		dec V29E
 		dod S13
 		renderstrimmp
@@ -1096,7 +1093,7 @@
 		fcb $f0
 		clr V228
 		dec V277
-LC5D7		bra LC5D7
+LC5D7		bra LC5D7			wait forever (or until the IRQ does something)
 ; swi 13 routine
 LC5D9		ldu #V388
 		dec V2B7
@@ -1211,10 +1208,11 @@
 		stx 6,s				;
 		rts
 ; swi 19 routine
+; Render the
 LC6A4		clr V2B1
-		clearstatus
+		clearstatus			clear the status area
 ; swi 20 routine
-LC6A8		clearcommand
+LC6A8		clearcommand			clear the command area
 		ldd #$8080
 		std V24F
 		ldb V29E
@@ -1241,20 +1239,20 @@
 		bne LC6CC
 		clr V29C
 		rts
-LC6D7		pshs x,u
+LC6D7		pshs x,u			save registers
 		stb V22D
 		stb V29D
-		cleargfx2
-		dod S01
+		cleargfx2			clear second graphics screen
+		dod S01				go draw graphic
 		dec V2B4
-		sync
-		puls x,u,pc
+		sync				wait for an interrupt
+		puls x,u,pc			restore registers and return
 ; swi 22 routine
 LC6E6		jsr LD489
 		ldd #$12c
 		std 4,u
 		renderstrimmp
-		fcb $3c,$24,$58,$06	packed string "PREPARE!"
+		fcb $3c,$24,$58,$06		packed string "PREPARE!"
 		fcb $45,$d8
 		clr V2B7
 		dec V2B4
@@ -1770,156 +1768,181 @@
 		decb				are we done yet?
 		bne LCA51			brif not - do another
 		puls a,b,x,y,u,pc		restore registers and return
-LCA67		pshs a,b,x
-		clr ,s
-		clr 1,s
-		clr V2C1
-		std V2C2
-		beq LCA97
-		cmpd 2,s
-		bne LCA7C
-		inc ,s
-		bra LCA97
-LCA7C		ldx #16
-LCA7F		lsl V2C3
-		rol V2C2
-		rol V2C1
-		lsl 1,s
-		rol ,s
-		ldd V2C1
-		subd 2,s
-		bcs LCA93
-		std V2C1
-		inc 1,s
-LCA93		leax -1,x
-		bne LCA7F
-LCA97		puls a,b,x,pc
-LCA99		coma
-		comb
-		addd #1
-		rts
-LCA9F		pshs a,b,x
-		ldx V243
-		ldd ,s
-		bpl LCAAE
-		bsr LCA99
-		bsr LCA67
-		bsr LCA99
-		skip2
-LCAAE		bsr LCA67
-		std ,s
-		puls a,b,x,pc
-LCAB4		jmp LCB8A
-LCAB7		pshs a,b,x,y,u
-		inc V22D
-		beq LCAB4
-		lda V22D
-		sta V22E
-		ldd V235
-		subd V231
-		std V23E
-		bpl LCACB
-		bsr LCA99
-LCACB		std V243
-		ldd V233
-		subd V22F
-		std V241
-		bpl LCAD7
-		bsr LCA99
-LCAD7		cmpd V243
-		blt LCAE0
-		std V243
-		beq LCAB4
-LCAE0		ldd V23E
-		bsr LCA9F
-		std V23E
-		tfr a,b
-		sex
-		ldb #1
-		sta V23D
-		bpl LCAF0
-		negb
-LCAF0		stb V245
-		ldd V241
-		bsr LCA9F
-		std V241
-		tfr a,b
-		sex
-		ldb #$20
-		sta V240
-		bpl LCB02
-		negb
-LCB02		stb V246
-		ldd V231
-		std V237
-		ldd V22F
-		std V23A
-		lda #$80
-		sta V239
-		sta V23C
-		ldx 2,u
-		stx V249
-		ldx ,u
-		stx V247
-		ldd V23A
-		jsr LCA0C
-		leax d,x
-		ldd V237
-		jsr LD37F
-		leax d,x
-		ldu #LCB8E
-		ldy V243
-LCB2E		dec V22E
-		bne LCB54
-		lda V22D
-		sta V22E
-		tst V237
-		bne LCB54
-		cmpx V247
-		blo LCB54
-		cmpx V249
-		bhs LCB54
-		ldb V238
-		andb #7
-		lda b,u
-		tst V22C
-		beq LCB50
-		coma
-		anda ,x
-		skip2
-LCB50		ora ,x
-		sta ,x
-LCB54		lda V238
-		anda #$f8
-		sta V2C1
-		ldd V238
-		addd V23E
-		std V238
-		ldb V237
-		adcb V23D
-		stb V237
-		anda #$f8
-		cmpa V2C1
-		beq LCB70
-		ldb V245
-		leax b,x
-LCB70		ldd V23B
-		sta V2C1
-		addd V241
-		std V23B
-		ldb V23A
-		adcb V240
-		stb V23A
-		cmpa V2C1
-		beq LCB86
-		ldb V246
-		leax b,x
-LCB86		leay -1,y
-		bne LCB2E
-LCB8A		dec V22D
-		puls a,b,x,y,u,pc
-LCB8E		fcb $80,$40,$20,$10
-		fcb $08,$04,$02,$01
+; This routine divides a 16 bit unsigned value in D by a 16 bit unsigned value in X. The result
+; will be in D with the binary point to the right of A.
+LCA67		pshs a,b,x			make hole for result and save divisor
+		clr ,s				; initialize quotient
+		clr 1,s				;
+		clr V2C1			use V2C1 for extra precision on dividend
+		std V2C2			save dividend
+		beq LCA97			brif dividend is zero - nothing to do
+		cmpd 2,s			is dividend equal to divisor?
+		bne LCA7C			brif not
+		inc ,s				set quotient to 1.0
+		bra LCA97			go return
+LCA7C		ldx #16				we need to do 16 iterations
+LCA7F		lsl V2C3			; shift dividend
+		rol V2C2			;
+		rol V2C1			;
+		lsl 1,s				= shift quotient
+		rol ,s				=
+		ldd V2C1			get dividend high word
+		subd 2,s			subtract out divisor
+		bcs LCA93			brif it doesn't go
+		std V2C1			save new dividend residue
+		inc 1,s				record the fact that it went
+LCA93		leax -1,x			have we done all 16 bits?
+		bne LCA7F			brif not
+LCA97		puls a,b,x,pc			fetch result, restore registers, and return
+LCA99		coma				; do a one's complement of D
+		comb				;
+		addd #1				adding 1 turns it into negation
+		rts				return to caller
+LCA9F		pshs a,b,x			save registers
+		ldx V243			get number of pixels to draw
+		ldd ,s				get the difference
+		bpl LCAAE			brif positive
+		bsr LCA99			negate difference
+		bsr LCA67			divide by number of pixels
+		bsr LCA99			negate the result
+		skip2				skip next instruction
+LCAAE		bsr LCA67			divide by number of pixels
+		std ,s				save step value
+		puls a,b,x,pc			restore registers and return
+LCAB4		jmp LCB8A			go return from the line drawing routine
+; Draw a line from (V231,V22F) to (V235,V233) respecting the light level in the dungeon (V22D)
+; which is used as a step count between when to actually draw pixels.
+;
+; Variables used:
+; V22D	the current light level in the dungeon
+; V22E	how many pixels left before we actually draw another
+; V22F	input start Y
+; V231	input start X
+; V233	input end Y
+; V235	input end X
+; V237	X coordinate of pixel to be drawn (24 bits with 8 bits after binary point)
+; V23A	U cpprdomate of pixel to be drawn (24 bits with 8 bits after binary point)
+; V23D	24 bit X coordinate difference (per pixel)
+; V240	24 bit Y coordinate difference (per pixel)
+; V243	number of pixels to draw in the line
+; V245	the offset for when X coordinate goes to a new byte
+; V246	the offset for when Y coordinate goes to a new line
+; V247	the start address of the graphics screen area the line is within
+; V249	the end address of the graphics screen area the line is within
+; V2C1	a temporary scratch variable
+;
+; Note: V240+1 and V23D+1 are also used as temporary holding values for the
+; integer difference in the Y and X coordinates respectively.
+LCAB7		pshs a,b,x,y,u			save registers
+		inc V22D			are we completely dark?
+		beq LCAB4			brif so - we can short circuit drawing entirely
+		lda V22D			get light level in dungeon
+		sta V22E			save in working count (skip count for pixel drawing)
+		ldd V235			get end X coordinate
+		subd V231			subtract start X coordinate
+		std V23D+1			save coordinate difference
+		bpl LCACB			brif positive difference
+		bsr LCA99			negate the difference
+LCACB		std V243			save absolute value of X difference as pixel count
+		ldd V233			get end Y coordinate
+		subd V22F			subtract start Y coordinate
+		std V240+1			save coordinate difference
+		bpl LCAD7			brif positive difference
+		bsr LCA99			negate the difference
+LCAD7		cmpd V243			is the Y difference bigger than X?
+		blt LCAE0			brif not
+		std V243			save Y difference as pixel count
+		beq LCAB4			brif no pixels to draw - short circuit
+LCAE0		ldd V23D+1			get X difference
+		bsr LCA9F			calculate X stepping value
+		std V23D+1			save X stepping value
+		tfr a,b				save msb of difference
+		sex				sign extend it
+		ldb #1				X stepping value - 1 for ascending
+		sta V23D			sign extend stepping difference to 24 bits
+		bpl LCAF0			brif positive
+		negb				set stepping value to -1
+LCAF0		stb V245			save X byte stepping value
+		ldd V240+1			get Y difference
+		bsr LCA9F			calculate Y step value
+		std V240+1			save result
+		tfr a,b				save msb of difference
+		sex				sign extend it
+		ldb #$20			Y byte stepping value - 32 bytes per line, ascending
+		sta V240			sign extend the difference to 24 bits
+		bpl LCB02			brif positive
+		negb				negate the difference - -32 bytes per line, descending
+LCB02		stb V246			save Y byte stepping value
+		ldd V231			get start X coordinate
+		std V237			save in X coordinate counter
+		ldd V22F			get start Y coordinate
+		std V23A			save in Y coordinate counter
+		lda #$80			value for low 8 bits to make the values ".5"
+		sta V237+2			set X coordinate to ".5"
+		sta V23A+2			set Y coordinate to ".5"
+		ldx 2,u				get end of graphics area address
+		stx V249			save it for later
+		ldx ,u				get start of graphics area address
+		stx V247			save it for later
+		ldd V23A			get Y coordinate for pixel
+		jsr LCA0C			shift left 5 bits - 32 bytes per row
+		leax d,x			add to screen start address
+		ldd V237			get X coordinate for pixel
+		jsr LD37F			shift right 3 bits - 8 pixels per byte
+		leax d,x			add to row start address
+		ldu #LCB8E			point to table of pixel masks
+		ldy V243			get number of pixels to draw
+LCB2E		dec V22E			are we ready to draw another pixel (due to light level)?
+		bne LCB54			brif not
+		lda V22D			get light level
+		sta V22E			reset current "pixel delay"
+		tst V237			is X coordinate off the right of the screen?
+		bne LCB54			brif so
+		cmpx V247			is the pixel address before the start of the graphics area?
+		blo LCB54			brif so
+		cmpx V249			is the pixel address after the end of the graphics area?
+		bhs LCB54			brif so
+		ldb V237+1			get X coordinate lsb
+		andb #7				mask off low 3 bits for offset in byte
+		lda b,u				get pixel mask to use
+		tst V22C			currently using black background?
+		beq LCB50			brif so
+		coma				invert mask for white background
+		anda ,x				merge in existing graphics data
+		skip2				skip next instruction
+LCB50		ora ,x				merge in existing graphics data (black background)
+		sta ,x				save new graphics data on the screen
+LCB54		lda V237+1			get X coordinate lsb
+		anda #$f8			mask off the pixel offset in the byte
+		sta V2C1			save it for later
+		ldd V237+1			get X coordinate low bits
+		addd V23D+1			add in X difference
+		std V237+1			save new low bits for X coordinate
+		ldb V237			get X coordinate high bits
+		adcb V23D			add in difference high bits
+		stb V237			save new X coordinate high bits
+		anda #$f8			mask off pixel offset in data byte
+		cmpa V2C1			are we in the same byte?
+		beq LCB70			brif so
+		ldb V245			get byte X step value 
+		leax b,x			move pointer appropriately
+LCB70		ldd V23A+1			get Y coord low bits
+		sta V2C1			save screen Y coordinate
+		addd V240+1			add in Y step value low bits
+		std V23A+1			save new low bits
+		ldb V23A			get Y coord high bits
+		adcb V240			add in Y step value high bits
+		stb V23A			save new Y coord high bits
+		cmpa V2C1			are we on the same scren row?
+		beq LCB86			brif so
+		ldb V246			get Y byte step value
+		leax b,x			move pointer appropriately
+LCB86		leay -1,y			have we drawn all the pixels?
+		bne LCB2E			brif not - draw another
+LCB8A		dec V22D			compensate for "inc" above
+		puls a,b,x,y,u,pc		restore registers and return
+LCB8E		fcb $80,$40,$20,$10		pixels 0, 1, 2, 3 (left to right) in byte
+		fcb $08,$04,$02,$01		pixels 4, 5, 6, 7 (left to right) in byte
 LCB96		pshs a,x,u
 		ldx V211
 		ldu #V313