mads 1.9.0 build 21 (16 Aug 09)
Source: C:\nes\atalan\codebase\atalan\examples/hello_world.asm
     1 = 0600			_stdbuf equ 1536
     2 = 0653			_putchr_proc_adr equ 1619
     3 = 0600			_std_print_out___5 equ _stdbuf
     4 = 0653			_std_print_out___6 equ _putchr_proc_adr
     5 				   org $2e0
     6 FFFF> 02E0-02E1> 00 20	   dta a($2000)
     7 02E2			   org $2000
     8 				;### hello_world.atl(3) "Hello, World!"
     9 2000-214F> 20 38 20	   jsr _std_print_out
    10 2003 0D 48 65 6C 6C 6F +    dta b(13),c'Hello, World!'
    11 2011 80			   dta b(128)
    12 2012 00			   dta b(0)
    13 				;### hello_world.atl(4) ""
    14 2013 20 38 20		   jsr _std_print_out
    15 2016 80			   dta b(128)
    16 2017 00			   dta b(0)
    17 				;### hello_world.atl(5) "I am ATALAN!"
    18 2018 20 38 20		   jsr _std_print_out
    19 201B 0C 49 20 61 6D 20 +    dta b(12),c'I am ATALAN!'
    20 2028 80			   dta b(128)
    21 2029 00			   dta b(0)
    22 202A			_lbl4:
    23 202A 4C 2A 20		   jmp _lbl4
    24 202D			   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 202D			_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 202D			skip1
    31 					rts
    32
    33 202D			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 202D			_out_putchr .proc
    41 				;Write character from A to output.
    42 					
    43 202D AA					tax
    44 202E AD 47 03				lda $347	;ICPUTB+1
    45 2031 48					pha
    46 2032 AD 46 03				lda $346	;ICPUTB
    47 2035 48					pha
    48 2036 8A					txa
    49 2037 60					rts
    50
    51 				.endp
    25 2038			   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 2038			_std_print_adr .proc
    76
    77 						lda #<_adr_putchr
    78 						ldx #>_adr_putchr
    79 						clc
    80 						bcc system__print
    81 						.endp
    82 						
    83 2038			_std_print_out .proc
    84 2038 A9 2D				lda #<_out_putchr
    85 203A A2 20				ldx #>_out_putchr
    86 203C 18					clc											;TODO: No Jump, if we are directly before the _std_print
    87 203D 90 00				bcc system__print
    88 						.endp
    89
    90 				 .IF .NOT .DEF _EOL_CHAR
    91 						_EOL_CHAR equ 0
    92 				 .ENDIF
    93
    94 203F			system__print .proc
    95 203F 8D 53 06				sta _putchr_proc_adr 
    96 2042 8E 54 06				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 2045 68					pla
   105 2046 85 32				sta ?argptr
   106 2048 68					pla
   107 2049 85 33				sta ?argptr+1
   108 204B 20 A6 20				jsr _read_byte	;just skip next byte (that is part of return address)
   109
   110 						;Read command from input				
   111 204E			command
   112 204E 20 A6 20				jsr _read_byte
   113 2051 85 15				sta ?size
   114 2053 A8					tay				; to set the flags
   115 2054 F0 4C				beq done		;command 0 means end of sequence
   116 2056 10 3E				bpl str			;1..127 constant string
   117
   118 						;Write n-byte integer variable
   119
   120 2058 29 7F				and #$7f		; zero top bit (it's always 1)
   121 205A 85 15				sta ?size
   122 205C F0 30				beq eol			;size 0 means end of line (0 byte integer would be nonsense)
   123
   124 				    ;Read address of the variable
   125
   126 205E 20 A6 20				jsr _read_byte
   127 2061 85 34				sta ?varptr		
   128 2063 20 A6 20				jsr _read_byte
   129 2066 85 35				sta ?varptr+1
   130
   131 						.IF _SYS_PRINT_SIGNED = 1 
   132 						;signed number has bit 5 set
   133 2068 A9 00				lda #0
   134 206A 85 16				sta ?sign
   135 						
   136 206C A5 15				lda ?size
   137 206E 29 20		    and #32		;%00100000
   138 2070 F0 13		    beq unsigned
   139
   140 2072 A5 15				lda ?size	;size uses only 4 bits
   141 2074 29 1F				and #31		;%00011111
   142 2076 85 15				sta ?size
   143 				    
   144 2078 A8			    tay				;test, whether the integer is negative (i.e. top byte is negative)
   145 2079 88			    dey
   146 207A B1 34		    lda (?varptr),y
   147 207C 10 07		    bpl unsigned
   148 						 
   149 207E C6 16		    dec ?sign				;sign = $ff  (-1)
   150 						
   151 2080 A9 2D				lda #'-'
   152 2082 20 B1 20				jsr _std_putchr
   153 						
   154 2085			unsigned
   155 						.ENDIF
   156 						    
   157 2085 20 FF 20				jsr _std_bin_to_bcd
   158 2088 20 B5 20				jsr _std_print_hex
   159 208B 18					clc
   160 208C 90 C0				bcc command		;jmp command
   161 208E			eol
   162 208E A9 9B				lda #_EOL_CHAR	
   163 2090 20 B1 20				jsr _std_putchr
   164 2093 18					clc
   165 2094 90 B8				bcc command		;jmp command
   166 						;Write constant string (size is 1..127, already stored)
   167 									
   168 2096			str
   169 2096 20 A6 20				jsr _read_byte
   170 2099 20 B1 20				jsr _std_putchr
   171 209C C6 15				dec ?size
   172 209E D0 F6				bne str
   173 20A0 F0 AC				beq	command
   174 										
   175 20A2			done		
   176 20A2 6C 32 00				jmp (?argptr)	
   177 20A5 60					rts
   178 						
   179 20A6			_read_byte
   180 20A6 A0 00				ldy #0
   181 20A8 B1 32				lda (?argptr),y
   182 20AA E6 32				inc ?argptr
   183 20AC D0 02				bne skip
   184 20AE E6 33				inc ?argptr+1
   185 20B0			skip
   186 20B0 60					rts
   187 				.endp
   188
   189 20B1			_std_putchr .proc
   190 20B1 6C 53 06				jmp (_putchr_proc_adr)
   191 20B4 60					rts
   192 				.endp
   193
   194
   195 20B5			.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 20B5 A5 15				lda ?size
   206 20B7 85 39				sta ?aux
   207 20B9 A9 00				lda #0
   208 20BB 85 38				sta ?aux2		; number of non-zero digits on output
   209 20BD F0 12				beq _loop
   210 20BF			_outbyte
   211 20BF A4 39				ldy ?aux		
   212 20C1 B1 34				lda (?varptr),y
   213 20C3 48					pha
   214 20C4 4A					lsr
   215 20C5 4A					lsr
   216 20C6 4A					lsr
   217 20C7 4A					lsr
   218 20C8 20 DF 20				jsr _write_digit
   219 					
   220 20CB 68					pla
   221 20CC 29 0F				and #$0f
   222 20CE 20 DF 20				jsr _write_digit
   223 20D1			_loop	
   224 20D1 C6 39				dec ?aux
   225 20D3 10 EA				bpl _outbyte
   226 						
   227 						;If no character has been written, write at least one 0		
   228 20D5 A5 38				lda ?aux2
   229 20D7 D0 05				bne _no_empty
   230 20D9 A9 30				lda #48
   231 20DB 20 B1 20				jsr _std_putchr
   232 20DE			_no_empty
   233 				 
   234 20DE 60					rts
   235 					
   236 20DF			_write_digit
   237 				;In: a 4 bit digit
   238
   239 20DF AA					tax
   240 20E0 D0 04				bne _non_zero
   241 20E2 A5 38				lda ?aux2
   242 20E4 F0 08				beq _done		;this is zero and there has been no char before - no output
   243 20E6			_non_zero
   244 20E6 BD EF 20				lda hex,x
   245 20E9 20 B1 20				jsr _std_putchr
   246 20EC E6 38				inc ?aux2
   247 20EE			_done
   248 20EE 60					rts
   249
   250 20EF 30 31 32 33 34 35 + hex     dta c"0123456789ABCDEF"
   251
   252 				.endp
   253
   254 20FF			.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 20FF A4 15				ldy ?size
   272 2101 84 39				sty ?aux		; used to count later
   273 2103 C8					iny				;add space to result
   274 2104 84 15				sty ?size
   275 						
   276 						;Zero the destination buffer
   277 2106 A9 00				lda #0
   278 2108 99 FF 05		zero	sta _stdbuf-1,y
   279 210B 88					dey
   280 210C D0 FA				bne zero
   281 						
   282 						;**** We convert the number byte a time
   283 210E F8					sed
   284 						
   285 						;?aux = varptr(?aux)
   286 210F			bytes
   287 210F C6 39				dec ?aux
   288 2111 A4 39				ldy ?aux
   289 2113 B1 34				lda (?varptr),y	
   290 						.IF _SYS_PRINT_SIGNED = 1 
   291 2115 45 16				eor ?sign
   292 						.ENDIF	
   293 2117 85 38				sta ?aux2
   294 2119 38					sec				;set top bit to 1
   295 211A B0 12				bcs loop		
   296
   297 211C			shift_byte			
   298 211C A2 00				ldx #0
   299 211E A4 15				ldy ?size
   300 2120			bcd_mul2
   301 2120 BD 00 06				lda _stdbuf,x
   302 2123 7D 00 06				adc	_stdbuf,x			;buf2(x) = buf2(x) * 2 + carry
   303 2126 9D 00 06				sta _stdbuf,x
   304 2129 E8					inx
   305 212A 88					dey								;TODO: cpx ?size
   306 212B D0 F3				bne bcd_mul2
   307 							
   308 212D 18					clc
   309 212E 26 38		loop	rol ?aux2		;divide by two, if result is 0, end
   310 2130 D0 EA				bne shift_byte		
   311 						
   312 2132 A5 39				lda ?aux
   313 2134 D0 D9				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 2136 A5 16				lda ?sign
   319 2138 0A					asl
   320 2139 A2 00				ldx #0
   321 213B			carry1
   322 213B BD 00 06				lda _stdbuf,x
   323 213E 69 00				adc #0
   324 2140 9D 00 06				sta _stdbuf,x
   325 2143 E8					inx
   326 2144 B0 F5				bcs carry1
   327 						.ENDIF
   328 									
   329 2146 D8					cld		
   330 						
   331 2147 A9 00				lda #<_stdbuf
   332 2149 85 34				sta ?varptr	
   333 214B A9 06				lda #>_stdbuf
   334 214D 85 35				sta ?varptr+1	
   335 214F 60					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 2150			_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 2150			loop
   367 						lsr MUL1		;MUL1 = MUL1 / 2
   368 						bcc noadd
   369 						
   370 						clc					;RES = RES + (MUL2 * $ff)
   371 						adc MUL2	  
   372 2150			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 2150			_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 2150			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 2150			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 2150			_sys_div8  .proc
   450 				  ldx #08  ; For an 8-bit divisor
   451 					
   452 2150			_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 2150			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 2150			addit 
   473 				  adc divisor
   474 2150			next  
   475 				  dex
   476 				  bne dloop
   477 				  bcs fini
   478 				  adc divisor
   479 				  clc
   480 2150			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 2150			_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 2150			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 2150			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 2150			Next
   568 					DEX			; decrement bit pair count
   569 					BNE	Loop		; loop if not all done
   570
   571 					lda Root
   572 					
   573 					RTS
   574 				.endp
    26
