mads 1.9.0 build 21 (16 Aug 09)
Source: C:\nes\atalan\codebase\atalan\examples/rainbow.asm
     1 = D40B			VCOUNT equ 54283
     2 = D40A			WSYNC equ 54282
     3 = 0000			button__state__pressed equ 0
     4 = 0284			STRIG equ 644
     5 = D01A			COL__BK equ 53274
     6 = D018			COL__PF2 equ 53272
     7 = 0014			RTCLOCK equ 20
     8 				   org $2e0
     9 FFFF> 02E0-02E1> 00 20	   dta a($2000)
    10 02E2			   org $2000
    11 				;### rainbow.atl(21) 	COL'PF2,COL'BK  = c,c
    12 2000-201C> 4C 13 20	   jmp _lbl4
    13 2003			_lbl6:
    14 				;### rainbow.atl(16) 	c:byte = lo (VCOUNT * 2 + RTCLOCK) 
    15 2003 AD 0B D4		   lda VCOUNT
    16 2006 0A			   asl
    17 2007 18			   clc
    18 2008 65 14		   adc RTCLOCK
    19 				;### rainbow.atl(17) 	wait'line   
    20 200A 8D 0A D4		   sta WSYNC
    21 				;### rainbow.atl(18) 	COL'PF2,COL'BK  = c,c
    22 200D 8D 18 D0		   sta COL__PF2
    23 2010 8D 1A D0		   sta COL__BK
    24 2013			_lbl4:
    25 				;### rainbow.atl(15) until STRIG(0) = pressed
    26 2013 AD 84 02		   lda STRIG-0
    27 2016 C9 00		   cmp #button__state__pressed
    28 2018 D0 E9		   jne _lbl6
    29 201A			_lbl5:
    30 201A 4C 1A 20		   jmp _lbl5
    31 201D			   icl 'C:\nes\atalan\codebase\atalan\platform\atari\atari.asm'
Source: C:\nes\atalan\codebase\atalan\platform\atari\atari.asm
     1 = 009B			_EOL_CHAR equ 155
     2
     3 201D			_adr_putchr .proc
     4 				;Write byte to address specified by _arr and increment the address by 1.
     5 					
     6 					;Convert ASCII to ATARI character screen code
     7 					;
     8 					;The rules are following:
     9 					;0..31   +64
    10 					;32..95  -32
    11 					;96..127 0  (codes are same)
    12 					 	
    13 					tax
    14 					rol 
    15 					rol 
    16 					rol 
    17 					rol
    18 					and #3
    19 					tay
    20 					txa
    21 					eor tbl,y
    22 					
    23 					;write the char to specified address	
    24 					ldy #0
    25 					sta (_arr),y
    26
    27 					inc _arr
    28 					bne skip1
    29 					inc _arr+1
    30 201D			skip1
    31 					rts
    32
    33 201D			tbl	dta b(%01000000)		;%0 00 00000		00..31	+64
    34 						dta b(%00100000)		;%0 01 00000		32..63	-32
    35 						dta b(%01100000)		;%0 10 00000    64..95  -32
    36 						dta b(%00000000)		;%0 11 00000    96..127 0
    37 					
    38 				.endp
    39
    40 201D			_out_putchr .proc
    41 				;Write character from A to output.
    42 					
    43 						tax
    44 						lda $347	;ICPUTB+1
    45 						pha
    46 						lda $346	;ICPUTB
    47 						pha
    48 						txa
    49 						rts
    50
    51 				.endp
    32 201D			   icl 'C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm'
Source: C:\nes\atalan\codebase\atalan\processor\m6502\m6502.asm
     1 				;********************************************************
     2 				;
     3 				;  ATALAN 6502 processor definition support routines
     4 				;
     5 				;  Parts of code have been taken from Effectus library.
     6 				;
     7 				;********************************************************
     8
     9
    10 				;Noname
    11 				;651-655         028B-028F
    12 				;
    13 				;More spare bytes that you shouldn't use because future versions of the OS might use them. 
    14 				;Locations 651 and 652 are already used by version "B" as part of the interrupt handler routines.
    15
    16 				;OS registers we use as temporary:
    17 				;
    18 				;BUFRLO,BUFRHI   50,51 $32,$33
    19 				;BFENLO,BFENHI   52,53 $34,$35
    20
    21 				;ADRESS  100,101   $64,$65  A temporary storage location used by the display handler for so many things that it made my mind spin and I forgot what they were.
    22 				;MLTTMP  102,103   $66,$67  More temporary storage, with aliases OPNTMP and TOADR.
    23 				;SAVADR  104,105   $68,$69  Also know as FRMADR. Also used for temporary storage. 
    24 				;                           Also not significant enough to explain (look at the OS listing if, for some reason, you really care).
    25
    26 				;Registers used when drawing line
    27 				;ROWAC   112,113   $70,$71
    28 				;COLAC   114,115   $72,$73
    29 				;ENDPT   116,117   $74,$75
    30 				;COUNTR  126,127   $7E,$7F  COUNTR tells how many points have to be plotted before the line is finished.
    31
    32 				;
    33 = 0001			_SYS_PRINT_SIGNED = 1
    34
    35 				;_putchr_proc_adr = $653  ;+$654 
    36 = 0032			?argptr =   $32		;$33
    37 = 0034			?varptr	=	$34
    38 = 0015			?size	=	$15		;15		//$16 BUFADR
    39 						.IF _SYS_PRINT_SIGNED = 1 
    40 = 0016			?sign = $16
    41 						.ENDIF
    42 = 0038			?aux2	=	$38
    43 = 0039			?aux	=	$39
    44
    45 				;Following assigns are defined by mc6502.atl
    46
    47 = 0032			_TW1 = $32	;$33  mc6502.atl defines these labels as _TEMPW1, _TEMPW2.
    48 = 0034			_TW2 = $34  ;$35
    49 = 0015			_TW3 = $15	;$16
    50 = 0070			_TL1 = $70	;four byte register ($70-$73) defined under name _TEMPL1 in mc6502.atl		
    51
    52 				;_stdbuf    = $600		;TODO: use LINBUF ($247-$26E)  (100,101 can point to it)
    53
    54 				;Argument list:
    55 				;	0	end of list
    56 				;	1..127  Constant string of specified lenght
    57 				; 128     EOL
    58
    59 				;   7 6 5 4 3 2 1 0 
    60 				;  |0| | | | | | | |		String of length 0..127
    61 				;  |1|0|0| | | | | |    Pointer to unsigned integer of length 1..31 bytes
    62 				;  |1|0|1| | | | | |    Pointer to signed integer of length 1..31 bytes
    63
    64 				;         ---- ptr to unsigned integer ---- (7-bit set)
    65 				; 129     one byte integer reference
    66 				;	130     two byte integer reference
    67 				;	131     three byte integer eference
    68 				;	132     four byte integer reference
    69 				;         ---- ptr to signed -----  (5-bit set)
    70 				; 161     one byte
    71 				; 162     two byte
    72 				; 163     three byte
    73 				; 164     four byte
    74
    75 201D			_std_print_adr .proc
    76
    77 						lda #<_adr_putchr
    78 						ldx #>_adr_putchr
    79 						clc
    80 						bcc system__print
    81 						.endp
    82 						
    83 201D			_std_print_out .proc
    84 						lda #<_out_putchr
    85 						ldx #>_out_putchr
    86 						clc											;TODO: No Jump, if we are directly before the _std_print
    87 						bcc system__print
    88 						.endp
    89
    90 				 .IF .NOT .DEF _EOL_CHAR
    91 						_EOL_CHAR equ 0
    92 				 .ENDIF
    93
    94 201D			system__print .proc
    95 						sta _putchr_proc_adr 
    96 						stx _putchr_proc_adr+1
    97 						
    98 						;... continue to _std_print
    99 				;.endp
   100 						
   101 				;_std_print	.proc
   102
   103 						;Get address of argument from stack
   104 						pla
   105 						sta ?argptr
   106 						pla
   107 						sta ?argptr+1
   108 						jsr _read_byte	;just skip next byte (that is part of return address)
   109
   110 						;Read command from input				
   111 201D			command
   112 						jsr _read_byte
   113 						sta ?size
   114 						tay				; to set the flags
   115 						beq done		;command 0 means end of sequence
   116 						bpl str			;1..127 constant string
   117
   118 						;Write n-byte integer variable
   119
   120 						and #$7f		; zero top bit (it's always 1)
   121 						sta ?size
   122 						beq eol			;size 0 means end of line (0 byte integer would be nonsense)
   123
   124 				    ;Read address of the variable
   125
   126 						jsr _read_byte
   127 						sta ?varptr		
   128 						jsr _read_byte
   129 						sta ?varptr+1
   130
   131 						.IF _SYS_PRINT_SIGNED = 1 
   132 						;signed number has bit 5 set
   133 						lda #0
   134 						sta ?sign
   135 						
   136 						lda ?size
   137 				    and #32		;%00100000
   138 				    beq unsigned
   139
   140 						lda ?size	;size uses only 4 bits
   141 						and #31		;%00011111
   142 						sta ?size
   143 				    
   144 				    tay				;test, whether the integer is negative (i.e. top byte is negative)
   145 				    dey
   146 				    lda (?varptr),y
   147 				    bpl unsigned
   148 						 
   149 				    dec ?sign				;sign = $ff  (-1)
   150 						
   151 						lda #'-'
   152 						jsr _std_putchr
   153 						
   154 201D			unsigned
   155 						.ENDIF
   156 						    
   157 						jsr _std_bin_to_bcd
   158 						jsr _std_print_hex
   159 						clc
   160 						bcc command		;jmp command
   161 201D			eol
   162 						lda #_EOL_CHAR	
   163 						jsr _std_putchr
   164 						clc
   165 						bcc command		;jmp command
   166 						;Write constant string (size is 1..127, already stored)
   167 									
   168 201D			str
   169 						jsr _read_byte
   170 						jsr _std_putchr
   171 						dec ?size
   172 						bne str
   173 						beq	command
   174 										
   175 201D			done		
   176 						jmp (?argptr)	
   177 						rts
   178 						
   179 201D			_read_byte
   180 						ldy #0
   181 						lda (?argptr),y
   182 						inc ?argptr
   183 						bne skip
   184 						inc ?argptr+1
   185 201D			skip
   186 						rts
   187 				.endp
   188
   189 201D			_std_putchr .proc
   190 						jmp (_putchr_proc_adr)
   191 						rts
   192 				.endp
   193
   194
   195 201D			.proc _std_print_hex
   196 				;Print hexadecimal number of arbitrary length (?size). 
   197 				;Leading zeroes are not printed.
   198 				;In:
   199 				;	?varptr		Pointer to memory containing the number
   200 				;	?size		  Number of bytes to print
   201 				;Uses:
   202 				;	?aux
   203 				;	?aux2
   204 					
   205 						lda ?size
   206 						sta ?aux
   207 						lda #0
   208 						sta ?aux2		; number of non-zero digits on output
   209 						beq _loop
   210 201D			_outbyte
   211 						ldy ?aux		
   212 						lda (?varptr),y
   213 						pha
   214 						lsr
   215 						lsr
   216 						lsr
   217 						lsr
   218 						jsr _write_digit
   219 					
   220 						pla
   221 						and #$0f
   222 						jsr _write_digit
   223 201D			_loop	
   224 						dec ?aux
   225 						bpl _outbyte
   226 						
   227 						;If no character has been written, write at least one 0		
   228 						lda ?aux2
   229 						bne _no_empty
   230 						lda #48
   231 						jsr _std_putchr
   232 201D			_no_empty
   233 				 
   234 						rts
   235 					
   236 201D			_write_digit
   237 				;In: a 4 bit digit
   238
   239 						tax
   240 						bne _non_zero
   241 						lda ?aux2
   242 						beq _done		;this is zero and there has been no char before - no output
   243 201D			_non_zero
   244 						lda hex,x
   245 						jsr _std_putchr
   246 						inc ?aux2
   247 201D			_done
   248 						rts
   249
   250 201D			hex     dta c"0123456789ABCDEF"
   251
   252 				.endp
   253
   254 201D			.proc _std_bin_to_bcd
   255 				;Convert binary number to BCD. 
   256 				;Arbitrary size (up to 127 bytes) are supported.
   257 				;In:
   258 				;	?varptr	pointer to binary number
   259 				; a   0 means unsigned number
   260 				;     $ff means signed number
   261 				;	?size	number of bytes
   262 				;Out:
   263 				;	?size	on output, returns size of resulting bcd number
   264 				;   ?varptr	on output, containg pointer to converted BCD
   265 				;Uses:
   266 				;	_stdbuf
   267 				;	?aux
   268 				;	?aux2
   269
   270 						;Compute size of resulting number 
   271 						ldy ?size
   272 						sty ?aux		; used to count later
   273 						iny				;add space to result
   274 						sty ?size
   275 						
   276 						;Zero the destination buffer
   277 						lda #0
   278 201D			zero	sta _stdbuf-1,y
   279 						dey
   280 						bne zero
   281 						
   282 						;**** We convert the number byte a time
   283 						sed
   284 						
   285 						;?aux = varptr(?aux)
   286 201D			bytes
   287 						dec ?aux
   288 						ldy ?aux
   289 						lda (?varptr),y	
   290 						.IF _SYS_PRINT_SIGNED = 1 
   291 						eor ?sign
   292 						.ENDIF	
   293 						sta ?aux2
   294 						sec				;set top bit to 1
   295 						bcs loop		
   296
   297 201D			shift_byte			
   298 						ldx #0
   299 						ldy ?size
   300 201D			bcd_mul2
   301 						lda _stdbuf,x
   302 						adc	_stdbuf,x			;buf2(x) = buf2(x) * 2 + carry
   303 						sta _stdbuf,x
   304 						inx
   305 						dey								;TODO: cpx ?size
   306 						bne bcd_mul2
   307 							
   308 						clc
   309 201D			loop	rol ?aux2		;divide by two, if result is 0, end
   310 						bne shift_byte		
   311 						
   312 						lda ?aux
   313 						bne bytes
   314 					
   315 						.IF _SYS_PRINT_SIGNED = 1 
   316 						;If this is negative number, add 1
   317 						;In case sign is $ff, asl will set the C to 1, otherwise to 0	
   318 						lda ?sign
   319 						asl
   320 						ldx #0
   321 201D			carry1
   322 						lda _stdbuf,x
   323 						adc #0
   324 						sta _stdbuf,x
   325 						inx
   326 						bcs carry1
   327 						.ENDIF
   328 									
   329 						cld		
   330 						
   331 						lda #<_stdbuf
   332 						sta ?varptr	
   333 						lda #>_stdbuf
   334 						sta ?varptr+1	
   335 						rts
   336
   337 				.endp
   338 				 
   339 				/*
   340 				  Mul8 - 8-bit multiplication routine
   341 				  
   342 				  Original source:
   343 				  Book Atari Roots (Chapter Ten - Assembly Language Math)
   344 				  Hyperlink: http://www.atariarchives.org/roots/chapter_10.php
   345 				  
   346 				  Parameters:
   347 					  a First multiplicant
   348 					  x Second multiplicant
   349 				  Result: 
   350 						TEMPW2,TEMPW2+1	high byte of the result
   351 				*/
   351
   352
   353 201D			_sys_mul8  .proc
   354
   355 = 0032			MUL1 = _TW1
   356 = 0033			MUL2 = _TW1+1
   357 = 0034			RES  = _TW2		;_TW2+1
   358
   359 						sta MUL1			;input comes in A and X
   360 						stx MUL2
   361 						
   362 						lda #0			;RES = 0
   363 						sta RES
   364 						
   365 						ldx #8  
   366 201D			loop
   367 						lsr MUL1		;MUL1 = MUL1 / 2
   368 						bcc noadd
   369 						
   370 						clc					;RES = RES + (MUL2 * $ff)
   371 						adc MUL2	  
   372 201D			noadd 
   373 						ror @				;RES = RES / 2		(Carry is 0, when there was no add)
   374 						ror RES
   375
   376 						dex
   377 						bne loop
   378 						
   379 						sta RES+1
   380 						rts
   381
   382 						.endp
   383
   384 				/*
   385 				  Mul16 - 16-bit multiplication routine
   386 				    
   387 				  Parameters:
   388 					  _TW1 First multiplicant (we use only two bytes now)
   389 					  _TW2 Second multiplicant
   390 				  Result: 
   391 						_TL1 Result
   392 				*/
   392
   393
   394 201D			_sys_mul16  .proc
   395
   396 = 0032			MUL1 = _TW1
   397 = 0034			MUL2 = _TW2
   398 = 0070			RES  = _TL1
   399 						
   400 						lda #0      ;RES = 0;
   401 						sta RES
   402 						sta RES+1
   403 						sta RES+2
   404 						sta RES+3
   405 						
   406 						ldx #16  
   407 201D			loop
   408 						lsr MUL1+1		;MUL1 = MUL1 / 2
   409 						ror MUL1
   410 						bcc noadd
   411 						
   412 						lda RES+2			;RES = RES + (MUL2 * $ffff)  (only upper half) 
   413 						clc		
   414 						adc MUL2
   415 						sta RES+2
   416 						lda RES+3
   417 						adc MUL2+1
   418 						sta RES+3
   419 201D			noadd
   420 						ror RES+3				;RES = RES / 2		(Carry is 0, when there was no add)
   421 						ror RES+2
   422 						ror RES+1
   423 						ror RES+0
   424 						 
   425 						dex
   426 						bne loop
   427 						
   428 						rts
   429
   430 						.endp
   431 				 
   432 				/*
   433 				  Div8 - 8-bit division routine
   434 				 
   435 				  Original source:
   436 				  Book Atari Roots (Chapter Ten - Assembly Language Math)
   437 				  Hyperlink: http://www.atariarchives.org/roots/chapter_10.php
   438 				 
   439 				  Parameters:
   440 				  _TEMPW1		16-bit dividend
   441 				  a					 p1_math:  8-bit divisor
   442 				  x          number of bits of an divisir (8 max)
   443 				 
   444 				  Result:
   445 				  x STORE1: 8-bit quotient
   446 				  a STORE2: 8-bit remainder
   447 				*/
   447
   448
   449 201D			_sys_div8  .proc
   450 				  ldx #08  ; For an 8-bit divisor
   451 					
   452 201D			_sys_div
   453
   454 = 0034			divisor  = _TW2
   455 = 0035			quotient = _TW2+1
   456
   457 				  sta divisor
   458 					lda #0     				; for non-8 divis
   459 					sta quotient
   460 				  lda _TW1+1
   461 				  sec 
   462 				  sbc divisor
   463 201D			dloop 
   464 				  php  					; The loop that divides 
   465 				  rol quotient
   466 				  asl _TW1
   467 				  rol @
   468 				  plp
   469 				  bcc addit
   470 				  sbc divisor
   471 				  jmp next
   472 201D			addit 
   473 				  adc divisor
   474 201D			next  
   475 				  dex
   476 				  bne dloop
   477 				  bcs fini
   478 				  adc divisor
   479 				  clc
   480 201D			fini
   481 					tax            ; x = remainder
   482 				  lda quotient   ; a = quotient
   483 				  rol
   484 				  rts
   485 				 
   486 				  .endp
   487
   488 				/*
   489
   490 				  Square Root
   491
   492 				  Calculates the 8 bit root and 9 bit remainder of a 16 bit unsigned integer in
   493 				  Numberl/Numberh. The result is always in the range 0 to 255 and is held in
   494 				  Root, the remainder is in the range 0 to 511 and is held in Reml/Remh
   495
   496 				  partial results are held in templ/temph
   497
   498 				  Destroys A, X registers.
   499
   500 					Arguments:
   501 					
   502 					_TEMPW1  16-bit number to compute root of
   503 					
   504 					<a        Square root
   505 					<_TEMPW2  Remainder of square
   506 					
   507 				*/
   507
   508
   509 201D			_sys_sqrt16 .proc
   510
   511 = 0032			Numberl		= _TW1		; number to find square root of low byte
   512 = 0033			Numberh		= _TW1+1	; number to find square root of high byte
   513 = 0034			Reml		  = _TW2		; remainder low byte
   514 = 0035			Remh		  = _TW2+1	; remainder high byte
   515 = 0070			templ		  = _TL1		; temp partial low byte
   516 = 0071			temph		  = _TL1+1	; temp partial high byte
   517 = 0072			Root		  = _TL1+2	; square root
   518
   519 					LDA	#$00		; clear A
   520 					STA	Reml		; clear remainder low byte
   521 					STA	Remh		; clear remainder high byte
   522 					STA	Root		; clear Root
   523 					LDX	#$08		; 8 pairs of bits to do
   524 201D			Loop
   525 					ASL	Root		; Root = Root * 2
   526
   527 					ASL	Numberl		; shift highest bit of number ..
   528 					ROL	Numberh		;
   529 					ROL	Reml		; .. into remainder
   530 					ROL	Remh		;
   531
   532 					ASL	Numberl		; shift highest bit of number ..
   533 					ROL	Numberh		;
   534 					ROL	Reml		; .. into remainder
   535 					ROL	Remh		;
   536
   537 					LDA	Root		; copy Root ..
   538 					STA	templ		; .. to templ
   539 					LDA	#$00		; clear byte
   540 					STA	temph		; clear temp high byte
   541
   542 					SEC			; +1
   543 					ROL	templ		; temp = temp * 2 + 1
   544 					ROL	temph		;
   545
   546 					LDA	Remh		; get remainder high byte
   547 					CMP	temph		; comapre with partial high byte
   548 					BCC	Next		; skip sub if remainder high byte smaller
   549
   550 					BNE	Subtr		; do sub if <> (must be remainder>partial !)
   551
   552 					LDA	Reml		; get remainder low byte
   553 					CMP	templ		; comapre with partial low byte
   554 					BCC	Next		; skip sub if remainder low byte smaller
   555
   556 								; else remainder>=partial so subtract then
   557 								; and add 1 to root. carry is always set here
   558 201D			Subtr
   559 					LDA	Reml		; get remainder low byte
   560 					SBC	templ		; subtract partial low byte
   561 					STA	Reml		; save remainder low byte
   562 					LDA	Remh		; get remainder high byte
   563 					SBC	temph		; subtract partial high byte
   564 					STA	Remh		; save remainder high byte
   565
   566 					INC	Root		; increment Root
   567 201D			Next
   568 					DEX			; decrement bit pair count
   569 					BNE	Loop		; loop if not all done
   570
   571 					lda Root
   572 					
   573 					RTS
   574 				.endp
    33
