<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.sizecoding.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wil</id>
		<title>SizeCoding - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://www.sizecoding.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wil"/>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/wiki/Special:Contributions/Wil"/>
		<updated>2026-05-02T03:09:09Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.0</generator>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1884</id>
		<title>Commodore 64</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1884"/>
				<updated>2025-12-25T11:41:32Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: grammar&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: ACME, Mads Assembler ( https://mads.atari8.info/ )&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
* Online Emulator + Assembler: https://lvllvl.com/c64/&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Rasters ====&lt;br /&gt;
Here is a piece of code that will generate a simple rasterbar.]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sprites ====&lt;br /&gt;
The commodore 64 has access to 8 different sprites of 24x21 (or 12x21 multicolor) resolution.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot in Bitmap modes ====&lt;br /&gt;
Here is a routine that will allow you to plot a single point in bitmap mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Text mode ====&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;function preserves A,X,Y and enables interrupt; you can also use the direct address $FC1A&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7               ;sets A to $F2, preserves X,Y&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay 1 Millisecond ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $EEB3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay ~ 2 Seconds ====&lt;br /&gt;
&lt;br /&gt;
This is actually Commodore's memory testing routine that is run every time the computer is turned on. Execution takes about 2 seconds to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $FD68&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=226987 Everything about illegal opcodes and their use in a C64 system&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1883</id>
		<title>Commodore 64</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1883"/>
				<updated>2025-12-25T11:41:03Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Text mode */ update details on newline call&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: ACME, Mads Assembler ( https://mads.atari8.info/ )&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
* Online Emulator + Assembler: https://lvllvl.com/c64/&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Rasters ====&lt;br /&gt;
Here is a piece of code that will generate a simple rasterbar.]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sprites ====&lt;br /&gt;
The commodore 64 has access to 8 different sprites of 24x21 (or 12x21 multicolor) resolution.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Plot in Bitmap modes ====&lt;br /&gt;
Here is a routine that will allow you to plot a single point in bitmap mode.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
To be added&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Text mode ====&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;function preserves A,X,Y and enables interrupt; you can also use the direct address $FC1A&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7               ;set A to $F2, preserves X,Y&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay 1 Millisecond ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $EEB3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay ~ 2 Seconds ====&lt;br /&gt;
&lt;br /&gt;
This is actually Commodore's memory testing routine that is run every time the computer is turned on. Execution takes about 2 seconds to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $FD68&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=226987 Everything about illegal opcodes and their use in a C64 system&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1281</id>
		<title>Commodore 64</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1281"/>
				<updated>2023-04-29T22:34:03Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Getting something on screen */ changed comment for clarity&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;function preserves A,X,Y and enables interrupt; you can also use the direct address $FC1A&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay 1 Millisecond ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $EEB3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay ~ 2 Seconds ====&lt;br /&gt;
&lt;br /&gt;
This is actually Commodore's memory testing routine that is run every time the computer is turned on. Execution takes about 2 seconds to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $FD68&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=226987 Everything about illegal opcodes and their use in a C64 system&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1220</id>
		<title>Christmas Tree</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1220"/>
				<updated>2023-01-07T16:40:21Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Remarkable Entries */ added shortest BASIC entry for C64&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4398/ Vintage Computing Christmas Challenge 2021] the goal was to create the shape of a given Christmas tree with as few bytes as possible. All platforms and languages were allowed.&lt;br /&gt;
The tree should be centered and look exactly like shown bellow.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
                          *&lt;br /&gt;
                         ***&lt;br /&gt;
                        *****&lt;br /&gt;
                       *******&lt;br /&gt;
                         ***&lt;br /&gt;
                       *******&lt;br /&gt;
                     ***********&lt;br /&gt;
                   ***************&lt;br /&gt;
                        ***** &lt;br /&gt;
                     *********** &lt;br /&gt;
                  ***************** &lt;br /&gt;
               *********************** &lt;br /&gt;
                         ***&lt;br /&gt;
                         ***&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* calculate stars using a formula&lt;br /&gt;
* save amount of stars within data&lt;br /&gt;
Do to the trunk of the tree, the second type was more efficient.&lt;br /&gt;
&lt;br /&gt;
The length measurement for the contest was defined as the shortest file size or source code. For Assembler, typically the filesize was relevant.&lt;br /&gt;
&lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest DOS version ===&lt;br /&gt;
The shortest DOS version with 44 bytes came from Hellmood:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=nasm&amp;gt;&lt;br /&gt;
org 100h			; define start of code for data access&lt;br /&gt;
db 80				; screen width, located at [SI], also &amp;quot;push ax&amp;quot;&lt;br /&gt;
mov bx,d			; pointer to data for XLAT&lt;br /&gt;
mov cx,1120			; 14 lines with 80 chars each&lt;br /&gt;
X: mov ax,cx		; get current sequence position&lt;br /&gt;
div byte [si]		; transform to [X,Y] in AL, AH&lt;br /&gt;
xlat				; get tree width for current line number&lt;br /&gt;
sub ah,40			; center tree in the middle&lt;br /&gt;
jnc F				; - &lt;br /&gt;
neg ah				; -&lt;br /&gt;
F: sub ah,al		; inside or outside tree?&lt;br /&gt;
salc				; set AL depending on carry flag&lt;br /&gt;
imul ax,byte -42	; transform into STAR or nothing&lt;br /&gt;
int 29h				; write current char to screen&lt;br /&gt;
loop X				; repeat unil all chars are plotter&lt;br /&gt;
ret					; return to prompt (JMP to former AX=0x0000)&lt;br /&gt;
d:db 2,2,12,9,6,3,8,6,4,2,4,3,2,1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Overall winning entry (C64) ===&lt;br /&gt;
The shortest entry overall was for the C64 and was submitted by Serato with only 37 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2092&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		lsr&lt;br /&gt;
		sbc #(128-width/2)&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bne -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
entry&lt;br /&gt;
		lax table,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;This entry to the Christmas Tree challenge saves space in six ways:&lt;br /&gt;
&lt;br /&gt;
 1. Using the c64 kernal routines to output individual characters and carriage return/linefeed. These routines save/restore X/Y allowing the registers to be used as loop counters.&lt;br /&gt;
&lt;br /&gt;
 2. Writing to the PNTR kernal variable to indent each row.&lt;br /&gt;
&lt;br /&gt;
 3. Using the unintended LAX opcode to simultaneously load A and X with the table values.&lt;br /&gt;
&lt;br /&gt;
 4. Storing the row widths as 2's complement negative values, to simplify the indent calculation to divide by two (LSR) and subtract (SBC). The inner loop simply counts back up to zero for rendering the correct width.&lt;br /&gt;
&lt;br /&gt;
 5. BASIC loads the Y register from address $30d. This defaults to 0 on startup, so Y does not need to be initialised.&lt;br /&gt;
&lt;br /&gt;
 6. Placing the table before the code in memory, the MSB of the table entries do not match the MSB of the first opcode (LSR), allowing the outer loop exit condition to be set implicitly in the Negative flag as the table values are read.&lt;br /&gt;
&lt;br /&gt;
These optimisations result in 37 bytes of machine code and data. Two further optimisations are possible each of which save 1 byte, but break the rules of this challenge:&lt;br /&gt;
&lt;br /&gt;
 a - LSR and SBC can be replaced with the unintended opcode ARR, if you are willing to &amp;quot;centre&amp;quot; the tree on a virtual screen width of 32 chars.&lt;br /&gt;
&lt;br /&gt;
 b - The BASIC interpreter leaves the line number in the zero page word at $39, so by setting the line number in the basic header to the address of the table, &amp;quot;LAX ($39),y&amp;quot; can be used for the table access.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shortest BASIC entry for C64 ===&lt;br /&gt;
&lt;br /&gt;
While BASIC programs seem to have a structural disadvantage for this kind of challenge, some entries in the spirit of oneliners are remarkably compact.&lt;br /&gt;
The shortest BASIC entry for the C64 was submitted by Wil with 68 bytes of source code length.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0dA-1,,1,2,,2,4,6,1,4,7,10,,:rEw:?sP18-w);:fOk=-2tow+w:?&amp;quot;*&amp;quot;;:nE:?:gO&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expanded source code as produced by LIST:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0 data-1,,1,2,,2,4,6,1,4,7,10,,:readw:printspc(18-w);:fork=-2tow+w:print&amp;quot;*&amp;quot;;:next:print:goto&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===36 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by altering the calculation so the default register values on start cause the first line to be drawn, shortening the table by one entry.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2074&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		adc	#width-1&lt;br /&gt;
		lsr&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
		lax table-1,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===35 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by writing directly to the screen, saving one byte. This change prevents the Y register being used as a loop index, so a different approach is used to iterate over the table entries.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2061&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
&lt;br /&gt;
code &lt;br /&gt;
--		adc	lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		dec	eal&lt;br /&gt;
		lax (eal+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
table&lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
;		!byte -1  &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by removing the ADC that indents the tree. Instead the offset to centre the tree is provided &amp;quot;for free&amp;quot; by suitably locating the EAL/EAH zp vector. This is possible because the C64 tape buffer is located immediately below the default screen location, and writing to the top line of the screen can be combined with the kernal routine to scroll the screen downward. This routine always leaves the Y register as $84, which added to the tape buffer zp vector TAPE1 points to the table ending at $3c0. The table values have their MSB clear so the centre of the tree has value $7f. When right shifted this gives an indent of $3f. The structure of the code is arranged so that the end of the binary ($3d4) plus the $3f offset give $413, ie 19 chars indented from the start of the screen matrix, centring the tree on screen. Although removing the ADC saves two bytes, one byte is lost adding the first row back into the table.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		scroll_down = $E981&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		tape1 = $b2&lt;br /&gt;
&lt;br /&gt;
*=$3c0-14&lt;br /&gt;
;		SYS 970 to draw tree&lt;br /&gt;
;		SYS 960 to draw with added &amp;quot;ground&amp;quot;&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
		!byte 128-1&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-15&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-17&lt;br /&gt;
		!byte 128-23&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
code &lt;br /&gt;
--		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (eal),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bpl -&lt;br /&gt;
entry&lt;br /&gt;
		jsr scroll_down&lt;br /&gt;
		dec	tape1&lt;br /&gt;
		lax (tape1),y&lt;br /&gt;
		bne --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===33 bytes version for C64===&lt;br /&gt;
This C64 version is most similar to the 35-byte version above. It saves 2 bytes over that version by omitting the table vector decrement, instead using the zp screen editor row variable which is incremented by the kernal with each linefeed. Fortuitously the high-byte of this vector is the &amp;quot;ASCII Value of Last Character Printed&amp;quot; which will always be $d. By locating the binary at $0dxx the table can be accessed with this vector.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		tblx = $D6&lt;br /&gt;
		&lt;br /&gt;
*=$d0b&lt;br /&gt;
;		SYS 3352 to draw tree, SYS should be typed with the cursor on row 10.&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -15&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -17&lt;br /&gt;
		!byte -23&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -3&lt;br /&gt;
code &lt;br /&gt;
--		adc lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		lax (tblx+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===32 bytes version for ZX Spectrum===&lt;br /&gt;
After the deadline following entry for the ZX Spectrum with only 35 bytes was submitted by reddie and optimized by char to 32 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
# Christmas Tree post-event build, ZX Spectrum version&lt;br /&gt;
# tnx to Manwe for info about event, better late than never =)&lt;br /&gt;
# first version - 35 bytes - (c) reddie, 2021.12.25&lt;br /&gt;
# optimized to 32 bytes by char, 2021.12.26 - huge thanks from me!&lt;br /&gt;
# full final object len  = 32 bytes (from label &amp;quot;data&amp;quot; to label &amp;quot;end&amp;quot;)&lt;br /&gt;
# data array len  = 14 bytes; code len = 18 bytes, 13 Z80 instructions&lt;br /&gt;
# execute from Basic only! entry point  = 61455, or just run TRD image&lt;br /&gt;
# and then run &amp;quot;ctree32b&amp;quot; Basic-program, it will load &amp;amp; start the code&lt;br /&gt;
# TRD file also contains source text in ALASM format&lt;br /&gt;
&lt;br /&gt;
data	org	-16*256+1&lt;br /&gt;
	defb	-5,-5,-25,-19,-13,-7,-17,-13,-9,-5,-9,-7,-5,-3&lt;br /&gt;
&lt;br /&gt;
start	dec	c&lt;br /&gt;
	jr	z,$	;stop when finished&lt;br /&gt;
	ld	a,23	;set coords via ROM procedure&lt;br /&gt;
	rst	16&lt;br /&gt;
	ld	a,(bc)	;line len in negative format&lt;br /&gt;
	ld	e,a&lt;br /&gt;
	rra&lt;br /&gt;
	sub	b	;centering&lt;br /&gt;
print	rst	16&lt;br /&gt;
	ld	a,&amp;quot;*&amp;quot;&lt;br /&gt;
	inc	e&lt;br /&gt;
	jr	nz,print&lt;br /&gt;
	jr	start&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===35 bytes version for Plus/4===&lt;br /&gt;
The following optimized version for the Commodore Plus/4 was created after the deadline:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502tasm&amp;quot;&amp;gt;&lt;br /&gt;
# Plus/4 Christmas Tree by GeirS, 2021-12-29&lt;br /&gt;
# Assemble using 64tass (replace hash with semicolon first, except for 'lda #$2a')&lt;br /&gt;
&lt;br /&gt;
BtmRow		= $0c00+$28*$18	# Address of bottom screen row&lt;br /&gt;
Screen		= BtmRow-$6c	# Adjustment for ($100-$28)/2&lt;br /&gt;
ScrollUp	= $da89			# KERNAL routine to scroll screen upwards&lt;br /&gt;
VarTab		= $2d			# Pointer: Start of BASIC variables&lt;br /&gt;
&lt;br /&gt;
			* = $1001		# Load address&lt;br /&gt;
&lt;br /&gt;
# BASIC stub&lt;br /&gt;
			.word (NextLine),0&lt;br /&gt;
			.null $9e,format(&amp;quot;%4d&amp;quot;, Start)&lt;br /&gt;
NextLine&lt;br /&gt;
			.word 0&lt;br /&gt;
&lt;br /&gt;
# Code and data (35 bytes total)&lt;br /&gt;
RowLoop&lt;br /&gt;
			lsr				# Calculate screen offset&lt;br /&gt;
			tay&lt;br /&gt;
			lda #$2a		# Asterisk character&lt;br /&gt;
CharLoop&lt;br /&gt;
			sta Screen,y	# Put char in bottom screen row&lt;br /&gt;
			iny&lt;br /&gt;
			inx&lt;br /&gt;
			bne CharLoop	# Loop for all chars&lt;br /&gt;
Start&lt;br /&gt;
			jsr ScrollUp	# Scroll the screen upwards (x=0 after)&lt;br /&gt;
			dec VarTab		# Adjust address of char count to use next&lt;br /&gt;
			lax (VarTab,x)	# Load char count into both a and x&lt;br /&gt;
			bmi RowLoop		# Loop while not done&lt;br /&gt;
			rts&lt;br /&gt;
&lt;br /&gt;
# Asterisk counts (negative values to optimize the code)&lt;br /&gt;
			.char -3,-3,-23,-17,-11,-5,-15,-11,-7,-3,-7,-5,-3,-1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1219</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1219"/>
				<updated>2023-01-07T16:37:27Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Shortest BASIC entry for C64 */ fixed language&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the shortest of source, file, or &amp;quot;real&amp;quot; code (excluding basic stub and file load address). For assembler programs started with SYS or call, typically filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shortest BASIC entry for C64 ===&lt;br /&gt;
&lt;br /&gt;
While BASIC programs seem to have a structural disadvantage for this kind of challenge, some entries in the spirit of oneliners are remarkably compact.&lt;br /&gt;
The shortest BASIC entry for the C64 was submitted by Serato with 68 bytes of tokenized code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0fOy=-8to8:fOx=-8to31:?cH(33+(9oraB(aB(y)-aB(x))&amp;gt;4oraB(x)&amp;gt;4aNaB(y)&amp;gt;4));:nE:nE&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expanded source code as produced by LIST:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0 fory=-8to8:forx=-8to31:printchr$(33+(9orabs(abs(y)-abs(x))&amp;gt;4orabs(x)&amp;gt;4andabs(y)&amp;gt;4));:next:next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===32 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column. The executable code size is 32 bytes (filesize 34 bytes).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1218</id>
		<title>Commodore 64</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Commodore_64&amp;diff=1218"/>
				<updated>2023-01-02T00:12:13Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: updated link to illegal opcaode manual to newest version&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay 1 Millisecond ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $EEB3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay ~ 2 Seconds ====&lt;br /&gt;
&lt;br /&gt;
This is actually Commodore's memory testing routine that is run every time the computer is turned on. Execution takes about 2 seconds to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $FD68&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=226987 Everything about illegal opcodes and their use in a C64 system&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1217</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1217"/>
				<updated>2023-01-01T23:06:57Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Shortest entry for C64 */ -&amp;gt; Shortest BASIC entry for C64&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the shortest of source, file, or &amp;quot;real&amp;quot; code (excluding basic stub and file load address). For assembler programs started with SYS or call, typically filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Shortest BASIC entry for C64 ===&lt;br /&gt;
&lt;br /&gt;
While BASIC programmes sees to have a structural disadvantage for this kind of challenge, some entries in the spirit of oneliners are remarably compact.&lt;br /&gt;
The shortest BASIC entry for the C64 was submitted by Serato with 68 bytes tokenized code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0fOy=-8to8:fOx=-8to31:?cH(33+(9oraB(aB(y)-aB(x))&amp;gt;4oraB(x)&amp;gt;4aNaB(y)&amp;gt;4));:nE:nE&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expanded source code as produced by LIST:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
0 fory=-8to8:forx=-8to31:printchr$(33+(9orabs(abs(y)-abs(x))&amp;gt;4orabs(x)&amp;gt;4andabs(y)&amp;gt;4));:next:next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===32 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column. The executable code size is 32 bytes (filesize 34 bytes).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1216</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1216"/>
				<updated>2023-01-01T22:49:53Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Winning Entries */ rephrased to avoid overuse of winning - there was only one main contest&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the shortest of source, file, or &amp;quot;real&amp;quot; code (excluding basic stub and file load address). For assembler programs started with SYS or call, typically filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===32 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column. The executable code size is 32 bytes (filesize 34 bytes).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1215</id>
		<title>Christmas Tree</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1215"/>
				<updated>2023-01-01T22:48:15Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* The challenge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4398/ Vintage Computing Christmas Challenge 2021] the goal was to create the shape of a given Christmas tree with as few bytes as possible. All platforms and languages were allowed.&lt;br /&gt;
The tree should be centered and look exactly like shown bellow.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
                          *&lt;br /&gt;
                         ***&lt;br /&gt;
                        *****&lt;br /&gt;
                       *******&lt;br /&gt;
                         ***&lt;br /&gt;
                       *******&lt;br /&gt;
                     ***********&lt;br /&gt;
                   ***************&lt;br /&gt;
                        ***** &lt;br /&gt;
                     *********** &lt;br /&gt;
                  ***************** &lt;br /&gt;
               *********************** &lt;br /&gt;
                         ***&lt;br /&gt;
                         ***&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* calculate stars using a formula&lt;br /&gt;
* save amount of stars within data&lt;br /&gt;
Do to the trunk of the tree, the second type was more efficient.&lt;br /&gt;
&lt;br /&gt;
The length measurement for the contest was defined as the shortest file size or source code. For Assembler, typically the filesize was relevant.&lt;br /&gt;
&lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest DOS version ===&lt;br /&gt;
The shortest DOS version with 44 bytes came from Hellmood:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=nasm&amp;gt;&lt;br /&gt;
org 100h			; define start of code for data access&lt;br /&gt;
db 80				; screen width, located at [SI], also &amp;quot;push ax&amp;quot;&lt;br /&gt;
mov bx,d			; pointer to data for XLAT&lt;br /&gt;
mov cx,1120			; 14 lines with 80 chars each&lt;br /&gt;
X: mov ax,cx		; get current sequence position&lt;br /&gt;
div byte [si]		; transform to [X,Y] in AL, AH&lt;br /&gt;
xlat				; get tree width for current line number&lt;br /&gt;
sub ah,40			; center tree in the middle&lt;br /&gt;
jnc F				; - &lt;br /&gt;
neg ah				; -&lt;br /&gt;
F: sub ah,al		; inside or outside tree?&lt;br /&gt;
salc				; set AL depending on carry flag&lt;br /&gt;
imul ax,byte -42	; transform into STAR or nothing&lt;br /&gt;
int 29h				; write current char to screen&lt;br /&gt;
loop X				; repeat unil all chars are plotter&lt;br /&gt;
ret					; return to prompt (JMP to former AX=0x0000)&lt;br /&gt;
d:db 2,2,12,9,6,3,8,6,4,2,4,3,2,1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Overall winning entry (C64) ===&lt;br /&gt;
The shortest entry overall was for the C64 and was submitted by Serato with only 37 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2092&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		lsr&lt;br /&gt;
		sbc #(128-width/2)&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bne -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
entry&lt;br /&gt;
		lax table,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;This entry to the Christmas Tree challenge saves space in six ways:&lt;br /&gt;
&lt;br /&gt;
 1. Using the c64 kernal routines to output individual characters and carriage return/linefeed. These routines save/restore X/Y allowing the registers to be used as loop counters.&lt;br /&gt;
&lt;br /&gt;
 2. Writing to the PNTR kernal variable to indent each row.&lt;br /&gt;
&lt;br /&gt;
 3. Using the unintended LAX opcode to simultaneously load A and X with the table values.&lt;br /&gt;
&lt;br /&gt;
 4. Storing the row widths as 2's complement negative values, to simplify the indent calculation to divide by two (LSR) and subtract (SBC). The inner loop simply counts back up to zero for rendering the correct width.&lt;br /&gt;
&lt;br /&gt;
 5. BASIC loads the Y register from address $30d. This defaults to 0 on startup, so Y does not need to be initialised.&lt;br /&gt;
&lt;br /&gt;
 6. Placing the table before the code in memory, the MSB of the table entries do not match the MSB of the first opcode (LSR), allowing the outer loop exit condition to be set implicitly in the Negative flag as the table values are read.&lt;br /&gt;
&lt;br /&gt;
These optimisations result in 37 bytes of machine code and data. Two further optimisations are possible each of which save 1 byte, but break the rules of this challenge:&lt;br /&gt;
&lt;br /&gt;
 a - LSR and SBC can be replaced with the unintended opcode ARR, if you are willing to &amp;quot;centre&amp;quot; the tree on a virtual screen width of 32 chars.&lt;br /&gt;
&lt;br /&gt;
 b - The BASIC interpreter leaves the line number in the zero page word at $39, so by setting the line number in the basic header to the address of the table, &amp;quot;LAX ($39),y&amp;quot; can be used for the table access.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===36 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by altering the calculation so the default register values on start cause the first line to be drawn, shortening the table by one entry.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2074&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		adc	#width-1&lt;br /&gt;
		lsr&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
		lax table-1,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===35 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by writing directly to the screen, saving one byte. This change prevents the Y register being used as a loop index, so a different approach is used to iterate over the table entries.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2061&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
&lt;br /&gt;
code &lt;br /&gt;
--		adc	lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		dec	eal&lt;br /&gt;
		lax (eal+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
table&lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
;		!byte -1  &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by removing the ADC that indents the tree. Instead the offset to centre the tree is provided &amp;quot;for free&amp;quot; by suitably locating the EAL/EAH zp vector. This is possible because the C64 tape buffer is located immediately below the default screen location, and writing to the top line of the screen can be combined with the kernal routine to scroll the screen downward. This routine always leaves the Y register as $84, which added to the tape buffer zp vector TAPE1 points to the table ending at $3c0. The table values have their MSB clear so the centre of the tree has value $7f. When right shifted this gives an indent of $3f. The structure of the code is arranged so that the end of the binary ($3d4) plus the $3f offset give $413, ie 19 chars indented from the start of the screen matrix, centring the tree on screen. Although removing the ADC saves two bytes, one byte is lost adding the first row back into the table.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		scroll_down = $E981&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		tape1 = $b2&lt;br /&gt;
&lt;br /&gt;
*=$3c0-14&lt;br /&gt;
;		SYS 970 to draw tree&lt;br /&gt;
;		SYS 960 to draw with added &amp;quot;ground&amp;quot;&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
		!byte 128-1&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-15&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-17&lt;br /&gt;
		!byte 128-23&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
code &lt;br /&gt;
--		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (eal),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bpl -&lt;br /&gt;
entry&lt;br /&gt;
		jsr scroll_down&lt;br /&gt;
		dec	tape1&lt;br /&gt;
		lax (tape1),y&lt;br /&gt;
		bne --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===33 bytes version for C64===&lt;br /&gt;
This C64 version is most similar to the 35-byte version above. It saves 2 bytes over that version by omitting the table vector decrement, instead using the zp screen editor row variable which is incremented by the kernal with each linefeed. Fortuitously the high-byte of this vector is the &amp;quot;ASCII Value of Last Character Printed&amp;quot; which will always be $d. By locating the binary at $0dxx the table can be accessed with this vector.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		tblx = $D6&lt;br /&gt;
		&lt;br /&gt;
*=$d0b&lt;br /&gt;
;		SYS 3352 to draw tree, SYS should be typed with the cursor on row 10.&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -15&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -17&lt;br /&gt;
		!byte -23&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -3&lt;br /&gt;
code &lt;br /&gt;
--		adc lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		lax (tblx+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===32 bytes version for ZX Spectrum===&lt;br /&gt;
After the deadline following entry for the ZX Spectrum with only 35 bytes was submitted by reddie and optimized by char to 32 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
# Christmas Tree post-event build, ZX Spectrum version&lt;br /&gt;
# tnx to Manwe for info about event, better late than never =)&lt;br /&gt;
# first version - 35 bytes - (c) reddie, 2021.12.25&lt;br /&gt;
# optimized to 32 bytes by char, 2021.12.26 - huge thanks from me!&lt;br /&gt;
# full final object len  = 32 bytes (from label &amp;quot;data&amp;quot; to label &amp;quot;end&amp;quot;)&lt;br /&gt;
# data array len  = 14 bytes; code len = 18 bytes, 13 Z80 instructions&lt;br /&gt;
# execute from Basic only! entry point  = 61455, or just run TRD image&lt;br /&gt;
# and then run &amp;quot;ctree32b&amp;quot; Basic-program, it will load &amp;amp; start the code&lt;br /&gt;
# TRD file also contains source text in ALASM format&lt;br /&gt;
&lt;br /&gt;
data	org	-16*256+1&lt;br /&gt;
	defb	-5,-5,-25,-19,-13,-7,-17,-13,-9,-5,-9,-7,-5,-3&lt;br /&gt;
&lt;br /&gt;
start	dec	c&lt;br /&gt;
	jr	z,$	;stop when finished&lt;br /&gt;
	ld	a,23	;set coords via ROM procedure&lt;br /&gt;
	rst	16&lt;br /&gt;
	ld	a,(bc)	;line len in negative format&lt;br /&gt;
	ld	e,a&lt;br /&gt;
	rra&lt;br /&gt;
	sub	b	;centering&lt;br /&gt;
print	rst	16&lt;br /&gt;
	ld	a,&amp;quot;*&amp;quot;&lt;br /&gt;
	inc	e&lt;br /&gt;
	jr	nz,print&lt;br /&gt;
	jr	start&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===35 bytes version for Plus/4===&lt;br /&gt;
The following optimized version for the Commodore Plus/4 was created after the deadline:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502tasm&amp;quot;&amp;gt;&lt;br /&gt;
# Plus/4 Christmas Tree by GeirS, 2021-12-29&lt;br /&gt;
# Assemble using 64tass (replace hash with semicolon first, except for 'lda #$2a')&lt;br /&gt;
&lt;br /&gt;
BtmRow		= $0c00+$28*$18	# Address of bottom screen row&lt;br /&gt;
Screen		= BtmRow-$6c	# Adjustment for ($100-$28)/2&lt;br /&gt;
ScrollUp	= $da89			# KERNAL routine to scroll screen upwards&lt;br /&gt;
VarTab		= $2d			# Pointer: Start of BASIC variables&lt;br /&gt;
&lt;br /&gt;
			* = $1001		# Load address&lt;br /&gt;
&lt;br /&gt;
# BASIC stub&lt;br /&gt;
			.word (NextLine),0&lt;br /&gt;
			.null $9e,format(&amp;quot;%4d&amp;quot;, Start)&lt;br /&gt;
NextLine&lt;br /&gt;
			.word 0&lt;br /&gt;
&lt;br /&gt;
# Code and data (35 bytes total)&lt;br /&gt;
RowLoop&lt;br /&gt;
			lsr				# Calculate screen offset&lt;br /&gt;
			tay&lt;br /&gt;
			lda #$2a		# Asterisk character&lt;br /&gt;
CharLoop&lt;br /&gt;
			sta Screen,y	# Put char in bottom screen row&lt;br /&gt;
			iny&lt;br /&gt;
			inx&lt;br /&gt;
			bne CharLoop	# Loop for all chars&lt;br /&gt;
Start&lt;br /&gt;
			jsr ScrollUp	# Scroll the screen upwards (x=0 after)&lt;br /&gt;
			dec VarTab		# Adjust address of char count to use next&lt;br /&gt;
			lax (VarTab,x)	# Load char count into both a and x&lt;br /&gt;
			bmi RowLoop		# Loop while not done&lt;br /&gt;
			rts&lt;br /&gt;
&lt;br /&gt;
# Asterisk counts (negative values to optimize the code)&lt;br /&gt;
			.char -3,-3,-23,-17,-11,-5,-15,-11,-7,-3,-7,-5,-3,-1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1214</id>
		<title>Christmas Tree</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1214"/>
				<updated>2023-01-01T22:44:47Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Winning Entries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4398/ Vintage Computing Christmas Challenge 2021] the goal was to create the shape of a given Christmas tree with as few bytes as possible. All platforms and languages were allowed.&lt;br /&gt;
The tree should be centered and look exactly like shown bellow.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
                          *&lt;br /&gt;
                         ***&lt;br /&gt;
                        *****&lt;br /&gt;
                       *******&lt;br /&gt;
                         ***&lt;br /&gt;
                       *******&lt;br /&gt;
                     ***********&lt;br /&gt;
                   ***************&lt;br /&gt;
                        ***** &lt;br /&gt;
                     *********** &lt;br /&gt;
                  ***************** &lt;br /&gt;
               *********************** &lt;br /&gt;
                         ***&lt;br /&gt;
                         ***&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* calculate stars using a formula&lt;br /&gt;
* save amount of stars within data&lt;br /&gt;
Do to the trunk of the tree, the second type was more efficient. &lt;br /&gt;
==Remarkable Entries==&lt;br /&gt;
=== Shortest DOS version ===&lt;br /&gt;
The shortest DOS version with 44 bytes came from Hellmood:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=nasm&amp;gt;&lt;br /&gt;
org 100h			; define start of code for data access&lt;br /&gt;
db 80				; screen width, located at [SI], also &amp;quot;push ax&amp;quot;&lt;br /&gt;
mov bx,d			; pointer to data for XLAT&lt;br /&gt;
mov cx,1120			; 14 lines with 80 chars each&lt;br /&gt;
X: mov ax,cx		; get current sequence position&lt;br /&gt;
div byte [si]		; transform to [X,Y] in AL, AH&lt;br /&gt;
xlat				; get tree width for current line number&lt;br /&gt;
sub ah,40			; center tree in the middle&lt;br /&gt;
jnc F				; - &lt;br /&gt;
neg ah				; -&lt;br /&gt;
F: sub ah,al		; inside or outside tree?&lt;br /&gt;
salc				; set AL depending on carry flag&lt;br /&gt;
imul ax,byte -42	; transform into STAR or nothing&lt;br /&gt;
int 29h				; write current char to screen&lt;br /&gt;
loop X				; repeat unil all chars are plotter&lt;br /&gt;
ret					; return to prompt (JMP to former AX=0x0000)&lt;br /&gt;
d:db 2,2,12,9,6,3,8,6,4,2,4,3,2,1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Overall winning entry (C64) ===&lt;br /&gt;
The shortest entry overall was for the C64 and was submitted by Serato with only 37 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2092&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		lsr&lt;br /&gt;
		sbc #(128-width/2)&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bne -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
entry&lt;br /&gt;
		lax table,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;This entry to the Christmas Tree challenge saves space in six ways:&lt;br /&gt;
&lt;br /&gt;
 1. Using the c64 kernal routines to output individual characters and carriage return/linefeed. These routines save/restore X/Y allowing the registers to be used as loop counters.&lt;br /&gt;
&lt;br /&gt;
 2. Writing to the PNTR kernal variable to indent each row.&lt;br /&gt;
&lt;br /&gt;
 3. Using the unintended LAX opcode to simultaneously load A and X with the table values.&lt;br /&gt;
&lt;br /&gt;
 4. Storing the row widths as 2's complement negative values, to simplify the indent calculation to divide by two (LSR) and subtract (SBC). The inner loop simply counts back up to zero for rendering the correct width.&lt;br /&gt;
&lt;br /&gt;
 5. BASIC loads the Y register from address $30d. This defaults to 0 on startup, so Y does not need to be initialised.&lt;br /&gt;
&lt;br /&gt;
 6. Placing the table before the code in memory, the MSB of the table entries do not match the MSB of the first opcode (LSR), allowing the outer loop exit condition to be set implicitly in the Negative flag as the table values are read.&lt;br /&gt;
&lt;br /&gt;
These optimisations result in 37 bytes of machine code and data. Two further optimisations are possible each of which save 1 byte, but break the rules of this challenge:&lt;br /&gt;
&lt;br /&gt;
 a - LSR and SBC can be replaced with the unintended opcode ARR, if you are willing to &amp;quot;centre&amp;quot; the tree on a virtual screen width of 32 chars.&lt;br /&gt;
&lt;br /&gt;
 b - The BASIC interpreter leaves the line number in the zero page word at $39, so by setting the line number in the basic header to the address of the table, &amp;quot;LAX ($39),y&amp;quot; can be used for the table access.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===36 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by altering the calculation so the default register values on start cause the first line to be drawn, shortening the table by one entry.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2074&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		adc	#width-1&lt;br /&gt;
		lsr&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
		lax table-1,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===35 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by writing directly to the screen, saving one byte. This change prevents the Y register being used as a loop index, so a different approach is used to iterate over the table entries.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2061&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
&lt;br /&gt;
code &lt;br /&gt;
--		adc	lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		dec	eal&lt;br /&gt;
		lax (eal+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
table&lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
;		!byte -1  &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by removing the ADC that indents the tree. Instead the offset to centre the tree is provided &amp;quot;for free&amp;quot; by suitably locating the EAL/EAH zp vector. This is possible because the C64 tape buffer is located immediately below the default screen location, and writing to the top line of the screen can be combined with the kernal routine to scroll the screen downward. This routine always leaves the Y register as $84, which added to the tape buffer zp vector TAPE1 points to the table ending at $3c0. The table values have their MSB clear so the centre of the tree has value $7f. When right shifted this gives an indent of $3f. The structure of the code is arranged so that the end of the binary ($3d4) plus the $3f offset give $413, ie 19 chars indented from the start of the screen matrix, centring the tree on screen. Although removing the ADC saves two bytes, one byte is lost adding the first row back into the table.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		scroll_down = $E981&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		tape1 = $b2&lt;br /&gt;
&lt;br /&gt;
*=$3c0-14&lt;br /&gt;
;		SYS 970 to draw tree&lt;br /&gt;
;		SYS 960 to draw with added &amp;quot;ground&amp;quot;&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
		!byte 128-1&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-15&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-17&lt;br /&gt;
		!byte 128-23&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
code &lt;br /&gt;
--		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (eal),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bpl -&lt;br /&gt;
entry&lt;br /&gt;
		jsr scroll_down&lt;br /&gt;
		dec	tape1&lt;br /&gt;
		lax (tape1),y&lt;br /&gt;
		bne --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===33 bytes version for C64===&lt;br /&gt;
This C64 version is most similar to the 35-byte version above. It saves 2 bytes over that version by omitting the table vector decrement, instead using the zp screen editor row variable which is incremented by the kernal with each linefeed. Fortuitously the high-byte of this vector is the &amp;quot;ASCII Value of Last Character Printed&amp;quot; which will always be $d. By locating the binary at $0dxx the table can be accessed with this vector.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		tblx = $D6&lt;br /&gt;
		&lt;br /&gt;
*=$d0b&lt;br /&gt;
;		SYS 3352 to draw tree, SYS should be typed with the cursor on row 10.&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -15&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -17&lt;br /&gt;
		!byte -23&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -3&lt;br /&gt;
code &lt;br /&gt;
--		adc lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		lax (tblx+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===32 bytes version for ZX Spectrum===&lt;br /&gt;
After the deadline following entry for the ZX Spectrum with only 35 bytes was submitted by reddie and optimized by char to 32 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
# Christmas Tree post-event build, ZX Spectrum version&lt;br /&gt;
# tnx to Manwe for info about event, better late than never =)&lt;br /&gt;
# first version - 35 bytes - (c) reddie, 2021.12.25&lt;br /&gt;
# optimized to 32 bytes by char, 2021.12.26 - huge thanks from me!&lt;br /&gt;
# full final object len  = 32 bytes (from label &amp;quot;data&amp;quot; to label &amp;quot;end&amp;quot;)&lt;br /&gt;
# data array len  = 14 bytes; code len = 18 bytes, 13 Z80 instructions&lt;br /&gt;
# execute from Basic only! entry point  = 61455, or just run TRD image&lt;br /&gt;
# and then run &amp;quot;ctree32b&amp;quot; Basic-program, it will load &amp;amp; start the code&lt;br /&gt;
# TRD file also contains source text in ALASM format&lt;br /&gt;
&lt;br /&gt;
data	org	-16*256+1&lt;br /&gt;
	defb	-5,-5,-25,-19,-13,-7,-17,-13,-9,-5,-9,-7,-5,-3&lt;br /&gt;
&lt;br /&gt;
start	dec	c&lt;br /&gt;
	jr	z,$	;stop when finished&lt;br /&gt;
	ld	a,23	;set coords via ROM procedure&lt;br /&gt;
	rst	16&lt;br /&gt;
	ld	a,(bc)	;line len in negative format&lt;br /&gt;
	ld	e,a&lt;br /&gt;
	rra&lt;br /&gt;
	sub	b	;centering&lt;br /&gt;
print	rst	16&lt;br /&gt;
	ld	a,&amp;quot;*&amp;quot;&lt;br /&gt;
	inc	e&lt;br /&gt;
	jr	nz,print&lt;br /&gt;
	jr	start&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===35 bytes version for Plus/4===&lt;br /&gt;
The following optimized version for the Commodore Plus/4 was created after the deadline:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502tasm&amp;quot;&amp;gt;&lt;br /&gt;
# Plus/4 Christmas Tree by GeirS, 2021-12-29&lt;br /&gt;
# Assemble using 64tass (replace hash with semicolon first, except for 'lda #$2a')&lt;br /&gt;
&lt;br /&gt;
BtmRow		= $0c00+$28*$18	# Address of bottom screen row&lt;br /&gt;
Screen		= BtmRow-$6c	# Adjustment for ($100-$28)/2&lt;br /&gt;
ScrollUp	= $da89			# KERNAL routine to scroll screen upwards&lt;br /&gt;
VarTab		= $2d			# Pointer: Start of BASIC variables&lt;br /&gt;
&lt;br /&gt;
			* = $1001		# Load address&lt;br /&gt;
&lt;br /&gt;
# BASIC stub&lt;br /&gt;
			.word (NextLine),0&lt;br /&gt;
			.null $9e,format(&amp;quot;%4d&amp;quot;, Start)&lt;br /&gt;
NextLine&lt;br /&gt;
			.word 0&lt;br /&gt;
&lt;br /&gt;
# Code and data (35 bytes total)&lt;br /&gt;
RowLoop&lt;br /&gt;
			lsr				# Calculate screen offset&lt;br /&gt;
			tay&lt;br /&gt;
			lda #$2a		# Asterisk character&lt;br /&gt;
CharLoop&lt;br /&gt;
			sta Screen,y	# Put char in bottom screen row&lt;br /&gt;
			iny&lt;br /&gt;
			inx&lt;br /&gt;
			bne CharLoop	# Loop for all chars&lt;br /&gt;
Start&lt;br /&gt;
			jsr ScrollUp	# Scroll the screen upwards (x=0 after)&lt;br /&gt;
			dec VarTab		# Adjust address of char count to use next&lt;br /&gt;
			lax (VarTab,x)	# Load char count into both a and x&lt;br /&gt;
			bmi RowLoop		# Loop while not done&lt;br /&gt;
			rts&lt;br /&gt;
&lt;br /&gt;
# Asterisk counts (negative values to optimize the code)&lt;br /&gt;
			.char -3,-3,-23,-17,-11,-5,-15,-11,-7,-3,-7,-5,-3,-1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1213</id>
		<title>Christmas Tree</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Tree&amp;diff=1213"/>
				<updated>2023-01-01T22:44:34Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Winning entry for C64 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4398/ Vintage Computing Christmas Challenge 2021] the goal was to create the shape of a given Christmas tree with as few bytes as possible. All platforms and languages were allowed.&lt;br /&gt;
The tree should be centered and look exactly like shown bellow.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
                          *&lt;br /&gt;
                         ***&lt;br /&gt;
                        *****&lt;br /&gt;
                       *******&lt;br /&gt;
                         ***&lt;br /&gt;
                       *******&lt;br /&gt;
                     ***********&lt;br /&gt;
                   ***************&lt;br /&gt;
                        ***** &lt;br /&gt;
                     *********** &lt;br /&gt;
                  ***************** &lt;br /&gt;
               *********************** &lt;br /&gt;
                         ***&lt;br /&gt;
                         ***&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* calculate stars using a formula&lt;br /&gt;
* save amount of stars within data&lt;br /&gt;
Do to the trunk of the tree, the second type was more efficient. &lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Shortest DOS version ===&lt;br /&gt;
The shortest DOS version with 44 bytes came from Hellmood:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=nasm&amp;gt;&lt;br /&gt;
org 100h			; define start of code for data access&lt;br /&gt;
db 80				; screen width, located at [SI], also &amp;quot;push ax&amp;quot;&lt;br /&gt;
mov bx,d			; pointer to data for XLAT&lt;br /&gt;
mov cx,1120			; 14 lines with 80 chars each&lt;br /&gt;
X: mov ax,cx		; get current sequence position&lt;br /&gt;
div byte [si]		; transform to [X,Y] in AL, AH&lt;br /&gt;
xlat				; get tree width for current line number&lt;br /&gt;
sub ah,40			; center tree in the middle&lt;br /&gt;
jnc F				; - &lt;br /&gt;
neg ah				; -&lt;br /&gt;
F: sub ah,al		; inside or outside tree?&lt;br /&gt;
salc				; set AL depending on carry flag&lt;br /&gt;
imul ax,byte -42	; transform into STAR or nothing&lt;br /&gt;
int 29h				; write current char to screen&lt;br /&gt;
loop X				; repeat unil all chars are plotter&lt;br /&gt;
ret					; return to prompt (JMP to former AX=0x0000)&lt;br /&gt;
d:db 2,2,12,9,6,3,8,6,4,2,4,3,2,1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Overall winning entry (C64) ===&lt;br /&gt;
The shortest entry overall was for the C64 and was submitted by Serato with only 37 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2092&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		lsr&lt;br /&gt;
		sbc #(128-width/2)&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bne -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
entry&lt;br /&gt;
		lax table,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;This entry to the Christmas Tree challenge saves space in six ways:&lt;br /&gt;
&lt;br /&gt;
 1. Using the c64 kernal routines to output individual characters and carriage return/linefeed. These routines save/restore X/Y allowing the registers to be used as loop counters.&lt;br /&gt;
&lt;br /&gt;
 2. Writing to the PNTR kernal variable to indent each row.&lt;br /&gt;
&lt;br /&gt;
 3. Using the unintended LAX opcode to simultaneously load A and X with the table values.&lt;br /&gt;
&lt;br /&gt;
 4. Storing the row widths as 2's complement negative values, to simplify the indent calculation to divide by two (LSR) and subtract (SBC). The inner loop simply counts back up to zero for rendering the correct width.&lt;br /&gt;
&lt;br /&gt;
 5. BASIC loads the Y register from address $30d. This defaults to 0 on startup, so Y does not need to be initialised.&lt;br /&gt;
&lt;br /&gt;
 6. Placing the table before the code in memory, the MSB of the table entries do not match the MSB of the first opcode (LSR), allowing the outer loop exit condition to be set implicitly in the Negative flag as the table values are read.&lt;br /&gt;
&lt;br /&gt;
These optimisations result in 37 bytes of machine code and data. Two further optimisations are possible each of which save 1 byte, but break the rules of this challenge:&lt;br /&gt;
&lt;br /&gt;
 a - LSR and SBC can be replaced with the unintended opcode ARR, if you are willing to &amp;quot;centre&amp;quot; the tree on a virtual screen width of 32 chars.&lt;br /&gt;
&lt;br /&gt;
 b - The BASIC interpreter leaves the line number in the zero page word at $39, so by setting the line number in the basic header to the address of the table, &amp;quot;LAX ($39),y&amp;quot; can be used for the table access.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===36 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by altering the calculation so the default register values on start cause the first line to be drawn, shortening the table by one entry.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		chrout = $FFD2&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pntr = $D3&lt;br /&gt;
		width = 40&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2074&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
code&lt;br /&gt;
--		adc	#width-1&lt;br /&gt;
		lsr&lt;br /&gt;
		sta pntr&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		jsr chrout&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf&lt;br /&gt;
		iny&lt;br /&gt;
		lax table-1,y&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===35 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by writing directly to the screen, saving one byte. This change prevents the Y register being used as a loop index, so a different approach is used to iterate over the table entries.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
		;; BASIC header&lt;br /&gt;
		*= $0801&lt;br /&gt;
		!word +, 10&lt;br /&gt;
		!byte $9e&lt;br /&gt;
		!text &amp;quot;2061&amp;quot;&lt;br /&gt;
		!byte 0&lt;br /&gt;
+		!word 0&lt;br /&gt;
&lt;br /&gt;
code &lt;br /&gt;
--		adc	lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		dec	eal&lt;br /&gt;
		lax (eal+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
table&lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -23 &lt;br /&gt;
		!byte -17 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -15 &lt;br /&gt;
		!byte -11 &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
		!byte -7  &lt;br /&gt;
		!byte -5  &lt;br /&gt;
		!byte -3  &lt;br /&gt;
;		!byte -1  &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
The C64 version was shortened by removing the ADC that indents the tree. Instead the offset to centre the tree is provided &amp;quot;for free&amp;quot; by suitably locating the EAL/EAH zp vector. This is possible because the C64 tape buffer is located immediately below the default screen location, and writing to the top line of the screen can be combined with the kernal routine to scroll the screen downward. This routine always leaves the Y register as $84, which added to the tape buffer zp vector TAPE1 points to the table ending at $3c0. The table values have their MSB clear so the centre of the tree has value $7f. When right shifted this gives an indent of $3f. The structure of the code is arranged so that the end of the binary ($3d4) plus the $3f offset give $413, ie 19 chars indented from the start of the screen matrix, centring the tree on screen. Although removing the ADC saves two bytes, one byte is lost adding the first row back into the table.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		scroll_down = $E981&lt;br /&gt;
		eal = $AE&lt;br /&gt;
		tape1 = $b2&lt;br /&gt;
&lt;br /&gt;
*=$3c0-14&lt;br /&gt;
;		SYS 970 to draw tree&lt;br /&gt;
;		SYS 960 to draw with added &amp;quot;ground&amp;quot;&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
		!byte 128-1&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-7&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-15&lt;br /&gt;
		!byte 128-5&lt;br /&gt;
		!byte 128-11&lt;br /&gt;
		!byte 128-17&lt;br /&gt;
		!byte 128-23&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
		!byte 128-3&lt;br /&gt;
code &lt;br /&gt;
--		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (eal),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bpl -&lt;br /&gt;
entry&lt;br /&gt;
		jsr scroll_down&lt;br /&gt;
		dec	tape1&lt;br /&gt;
		lax (tape1),y&lt;br /&gt;
		bne --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===33 bytes version for C64===&lt;br /&gt;
This C64 version is most similar to the 35-byte version above. It saves 2 bytes over that version by omitting the table vector decrement, instead using the zp screen editor row variable which is incremented by the kernal with each linefeed. Fortuitously the high-byte of this vector is the &amp;quot;ASCII Value of Last Character Printed&amp;quot; which will always be $d. By locating the binary at $0dxx the table can be accessed with this vector.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		!cpu 6510			; enable unintended opcodes in ACME assembler&lt;br /&gt;
&lt;br /&gt;
		crlf = $AAD7&lt;br /&gt;
		pnt = $D1&lt;br /&gt;
		lnmx = $D5&lt;br /&gt;
		tblx = $D6&lt;br /&gt;
		&lt;br /&gt;
*=$d0b&lt;br /&gt;
;		SYS 3352 to draw tree, SYS should be typed with the cursor on row 10.&lt;br /&gt;
&lt;br /&gt;
table&lt;br /&gt;
;		!byte -1&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -7&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -15&lt;br /&gt;
		!byte -5&lt;br /&gt;
		!byte -11&lt;br /&gt;
		!byte -17&lt;br /&gt;
		!byte -23&lt;br /&gt;
		!byte -3&lt;br /&gt;
		!byte -3&lt;br /&gt;
code &lt;br /&gt;
--		adc lnmx&lt;br /&gt;
		lsr&lt;br /&gt;
		tay&lt;br /&gt;
		lda #'*'&lt;br /&gt;
-		sta (pnt),y&lt;br /&gt;
		iny&lt;br /&gt;
		inx&lt;br /&gt;
		bmi -&lt;br /&gt;
		jsr crlf-8&lt;br /&gt;
		lax (tblx+1,x)&lt;br /&gt;
		bmi --&lt;br /&gt;
		rts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===32 bytes version for ZX Spectrum===&lt;br /&gt;
After the deadline following entry for the ZX Spectrum with only 35 bytes was submitted by reddie and optimized by char to 32 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
# Christmas Tree post-event build, ZX Spectrum version&lt;br /&gt;
# tnx to Manwe for info about event, better late than never =)&lt;br /&gt;
# first version - 35 bytes - (c) reddie, 2021.12.25&lt;br /&gt;
# optimized to 32 bytes by char, 2021.12.26 - huge thanks from me!&lt;br /&gt;
# full final object len  = 32 bytes (from label &amp;quot;data&amp;quot; to label &amp;quot;end&amp;quot;)&lt;br /&gt;
# data array len  = 14 bytes; code len = 18 bytes, 13 Z80 instructions&lt;br /&gt;
# execute from Basic only! entry point  = 61455, or just run TRD image&lt;br /&gt;
# and then run &amp;quot;ctree32b&amp;quot; Basic-program, it will load &amp;amp; start the code&lt;br /&gt;
# TRD file also contains source text in ALASM format&lt;br /&gt;
&lt;br /&gt;
data	org	-16*256+1&lt;br /&gt;
	defb	-5,-5,-25,-19,-13,-7,-17,-13,-9,-5,-9,-7,-5,-3&lt;br /&gt;
&lt;br /&gt;
start	dec	c&lt;br /&gt;
	jr	z,$	;stop when finished&lt;br /&gt;
	ld	a,23	;set coords via ROM procedure&lt;br /&gt;
	rst	16&lt;br /&gt;
	ld	a,(bc)	;line len in negative format&lt;br /&gt;
	ld	e,a&lt;br /&gt;
	rra&lt;br /&gt;
	sub	b	;centering&lt;br /&gt;
print	rst	16&lt;br /&gt;
	ld	a,&amp;quot;*&amp;quot;&lt;br /&gt;
	inc	e&lt;br /&gt;
	jr	nz,print&lt;br /&gt;
	jr	start&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===35 bytes version for Plus/4===&lt;br /&gt;
The following optimized version for the Commodore Plus/4 was created after the deadline:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502tasm&amp;quot;&amp;gt;&lt;br /&gt;
# Plus/4 Christmas Tree by GeirS, 2021-12-29&lt;br /&gt;
# Assemble using 64tass (replace hash with semicolon first, except for 'lda #$2a')&lt;br /&gt;
&lt;br /&gt;
BtmRow		= $0c00+$28*$18	# Address of bottom screen row&lt;br /&gt;
Screen		= BtmRow-$6c	# Adjustment for ($100-$28)/2&lt;br /&gt;
ScrollUp	= $da89			# KERNAL routine to scroll screen upwards&lt;br /&gt;
VarTab		= $2d			# Pointer: Start of BASIC variables&lt;br /&gt;
&lt;br /&gt;
			* = $1001		# Load address&lt;br /&gt;
&lt;br /&gt;
# BASIC stub&lt;br /&gt;
			.word (NextLine),0&lt;br /&gt;
			.null $9e,format(&amp;quot;%4d&amp;quot;, Start)&lt;br /&gt;
NextLine&lt;br /&gt;
			.word 0&lt;br /&gt;
&lt;br /&gt;
# Code and data (35 bytes total)&lt;br /&gt;
RowLoop&lt;br /&gt;
			lsr				# Calculate screen offset&lt;br /&gt;
			tay&lt;br /&gt;
			lda #$2a		# Asterisk character&lt;br /&gt;
CharLoop&lt;br /&gt;
			sta Screen,y	# Put char in bottom screen row&lt;br /&gt;
			iny&lt;br /&gt;
			inx&lt;br /&gt;
			bne CharLoop	# Loop for all chars&lt;br /&gt;
Start&lt;br /&gt;
			jsr ScrollUp	# Scroll the screen upwards (x=0 after)&lt;br /&gt;
			dec VarTab		# Adjust address of char count to use next&lt;br /&gt;
			lax (VarTab,x)	# Load char count into both a and x&lt;br /&gt;
			bmi RowLoop		# Loop while not done&lt;br /&gt;
			rts&lt;br /&gt;
&lt;br /&gt;
# Asterisk counts (negative values to optimize the code)&lt;br /&gt;
			.char -3,-3,-23,-17,-11,-5,-15,-11,-7,-3,-7,-5,-3,-1&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1212</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1212"/>
				<updated>2023-01-01T22:39:17Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* The challenge */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the shortest of source, file, or &amp;quot;real&amp;quot; code (excluding basic stub and file load address). For assembler programs started with SYS or call, typically filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Winning entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===32 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column. The executable code size is 32 bytes (filesize 34 bytes).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1211</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1211"/>
				<updated>2023-01-01T21:46:18Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* 34 bytes version for C64 */ -&amp;gt; should be called 32 bytes version&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the minimum of (executable code minus file header, sourcecode). For assembler, the sourcecode version is always larger than the program, so the filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Winning entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===32 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column. The executable code size is 32 bytes (filesize 34 bytes).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1210</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1210"/>
				<updated>2023-01-01T21:44:51Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Winning entry for C64 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the minimum of (executable code minus file header, sourcecode). For assembler, the sourcecode version is always larger than the program, so the filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Winning entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes (filesize 36 bytes):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1209</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1209"/>
				<updated>2023-01-01T21:44:18Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* The challenge */ Program length measurement explanation.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
Program length was measured as the minimum of (executable code minus file header, sourcecode). For assembler, the sourcecode version is always larger than the program, so the filesize minus load address (=file header) is the relevant number. A BASIC stub to start the program would also not count, but it is not allowed to use the stub#s side effects to initialize zeropage addresses.&lt;br /&gt;
&lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Winning entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
==Optimized post-deadline versions==&lt;br /&gt;
===34 bytes version for C64===&lt;br /&gt;
This shorter C64 version by Serato renders the union of four triangles rotated 90 degrees. To save space it iterates 256 times. It also uses a trick where the carry flag state is set by the loop comparison causing the active row to advance automatically at the end of each column.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        plot = $E50C    ; Kernal routine moves cursor to (y,x)&lt;br /&gt;
        sys_h = $15&lt;br /&gt;
        pnt = $d1&lt;br /&gt;
        tblx = $d6&lt;br /&gt;
* = $c00&lt;br /&gt;
        lda #22&lt;br /&gt;
loop2   tay&lt;br /&gt;
loop1   sbc #3&lt;br /&gt;
        tax&lt;br /&gt;
        jsr plot&lt;br /&gt;
        lda #'*'&lt;br /&gt;
        sta (pnt),y&lt;br /&gt;
        tya&lt;br /&gt;
        eor #$1f&lt;br /&gt;
        ldy tblx&lt;br /&gt;
        dec $2&lt;br /&gt;
        bne loop1&lt;br /&gt;
        dey&lt;br /&gt;
        cpy #10&lt;br /&gt;
        bcs loop1&lt;br /&gt;
        dec sys_h&lt;br /&gt;
        bpl loop2&lt;br /&gt;
        rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1207</id>
		<title>Christmas Star</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Christmas_Star&amp;diff=1207"/>
				<updated>2022-12-28T21:46:33Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* The challenge */ minor language edit&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Case Study]]&lt;br /&gt;
==The challenge==&lt;br /&gt;
During the [https://demozoo.org/parties/4578/ Vintage Computing Christmas Challenge 2022], the goal was to create the shape of a given Christmas star with as few bytes as possible. All platforms and languages were allowed. The star should look exactly like the one shown below.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
               *       *&lt;br /&gt;
               **     **&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **** ****&lt;br /&gt;
           *****************&lt;br /&gt;
            ***************&lt;br /&gt;
             *************&lt;br /&gt;
              ***********&lt;br /&gt;
               *********&lt;br /&gt;
              ***********&lt;br /&gt;
             *************&lt;br /&gt;
            ***************&lt;br /&gt;
           *****************&lt;br /&gt;
               **** ****&lt;br /&gt;
               ***   ***&lt;br /&gt;
               **     **&lt;br /&gt;
               *       *&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
There were mainly two kinds of approaches:&lt;br /&gt;
* draw character by character&lt;br /&gt;
* mirror triangles&lt;br /&gt;
&lt;br /&gt;
==Winning Entries==&lt;br /&gt;
=== Winning entry for C64 ===&lt;br /&gt;
The shortest entry for the C64 was submitted by Serato with 34 bytes:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
		chrout = $FFD2	; Kernal routine outputs char in A&lt;br /&gt;
		plot = $E50C	; Kernal routine moves cursor to (y,x)&lt;br /&gt;
		pnt = $d1     	; Pointer to the start of the current screen line&lt;br /&gt;
		row = $b3		; zp var that always starts at 3&lt;br /&gt;
*       = $1000&lt;br /&gt;
do_2nd	ldx row			; render &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
do_line	ldy #9			; render one row of triangle, and its x-mirror image&lt;br /&gt;
		jsr plot		; move cursor to column 9 on row x&lt;br /&gt;
		ldy #17			; start x-mirrored image in column 17&lt;br /&gt;
		lda #'*'		; the star of the show&lt;br /&gt;
do_asks	jsr chrout		; print star, advancing kernal cursor to right&lt;br /&gt;
		sta (pnt),y		; write x-mirrored star with mirrored cursor&lt;br /&gt;
		dey				; advance mirrored cursor to left&lt;br /&gt;
		cpy row			; test against line y=x to form diagonal&lt;br /&gt;
		bne do_asks		&lt;br /&gt;
		cpx row			; check if these weere &amp;quot;upside down&amp;quot; triangles&lt;br /&gt;
		bne do_2nd		; if not, draw the upside down ones&lt;br /&gt;
sys		lda #$10		; entry point of routine, start with y mirrored&lt;br /&gt;
		sec				; wish this could be avoided, feels like a wasted byte&lt;br /&gt;
		isc row			; increment bottom row counter and put (16 - row) in A&lt;br /&gt;
		tax				; we need in x register&lt;br /&gt;
		bpl do_line		; repeat until row &amp;lt; 0, then fall through&lt;br /&gt;
		rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Serato's entry was provided with the following comments:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;The required octogram may be constructed as the union of four right triangles reflected four ways in x and y axes. The central loop renders one row of the triangle, plus its horizontal mirror image. The outer loop iterates over normal and mirror image screen rows. The result is all four triangles being rendered on top of each other. Saved a byte with unintended 6502 opcode ISC which combines INC and SBC to both increment a counter and load (16 minus) its value into A.&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=1084</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=1084"/>
				<updated>2022-02-25T22:10:02Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Useful Kernal functions */ Delay routines&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay 1 Millisecond ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $EEB3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Delay ~ 2 Seconds ====&lt;br /&gt;
&lt;br /&gt;
This is actually Commodore's memory testing routine that is run every time the computer is turned on. Execution takes about 2 seconds to execute.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $FD68&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=198357 Everything about illegal opcodes and their use in a C64 system&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html (with an additional presentation at https://www.youtube.com/watch?v=kJ-EXbnw0kQ )&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 65C02 with custom hardware for graphics and sound.&amp;lt;br&amp;gt;&lt;br /&gt;
If you come from Atari 8bit or Apple, there are no illegal opcodes anymore like &amp;lt;b&amp;gt;lax&amp;lt;/b&amp;gt;, but a lot new nice opcodes like &amp;lt;b&amp;gt;stz&amp;lt;/b&amp;gt;. :-)&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler:&lt;br /&gt;
Below size coding examples use [https://github.com/42Bastian/lyxass Lyxass] as assembler and are using the in-official DevKit [https://github.com/42Bastian/new_bll new_bll].&amp;lt;br&amp;gt;&lt;br /&gt;
But any other 6502 assembler is suitable. Best if it can assemble the extra opcodes of the 65C02 (like &amp;lt;b&amp;gt;BBRx&amp;lt;/b&amp;gt; or &amp;lt;b&amp;gt;RMBx&amp;lt;/b&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
* Encryption&lt;br /&gt;
The Lynx boot ROM will decrypt the &amp;quot;boot sector&amp;quot; of a Lynx card image. It is done in chunks of 50bytes. Maximum are 5 chunks, hence the limit of a &amp;quot;boot sector&amp;quot; is 250 bytes, but the last byte &amp;lt;b&amp;gt;must&amp;lt;/b&amp;gt; be zero.&lt;br /&gt;
&lt;br /&gt;
* Emulator(s): &lt;br /&gt;
Currently Felix is the most accurate Emulator, though only running yet on Windows platform.&amp;lt;br&amp;gt;&lt;br /&gt;
It is currently available via [https://github.com/laoo/Felix GitHub].&lt;br /&gt;
&lt;br /&gt;
* Hardware:&lt;br /&gt;
If you want to run the code on a real Lynx, you should have a SD card for the Lynx (AgaCard, LynxGD) and also a USB&amp;lt;-&amp;gt;Serial adapter.&lt;br /&gt;
&lt;br /&gt;
=== Initial values ===&lt;br /&gt;
After the boot-rom has decrypted the boot-sector, some things are pre-defined:&lt;br /&gt;
* CPU Registers:&lt;br /&gt;
  A = 0&lt;br /&gt;
  X = 0&lt;br /&gt;
  Y = 2&lt;br /&gt;
  P = undefined&lt;br /&gt;
  S = undefined&lt;br /&gt;
&lt;br /&gt;
* Zero page&lt;br /&gt;
&lt;br /&gt;
 $00 - 0&lt;br /&gt;
 $01 - 0&lt;br /&gt;
 $02 - 0&lt;br /&gt;
&lt;br /&gt;
* Main&lt;br /&gt;
&lt;br /&gt;
Main memory is cleared to zero despite a $5000..%$50aa where decryption code is placed.&lt;br /&gt;
&lt;br /&gt;
* Bank switching&lt;br /&gt;
&lt;br /&gt;
ROM, Mikey and Suzy are mapped, Vector table is ROM.&lt;br /&gt;
&lt;br /&gt;
* Suzy&lt;br /&gt;
The sprite engine is &amp;lt;b&amp;gt;not&amp;lt;/b&amp;gt; setup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
The Lynx display is 160x102 pixels which are linear organized as 4 bit per pixel. Hence the screen is little less than 8K.&lt;br /&gt;
The pixel value is a pointer to the color look up table.&lt;br /&gt;
The Lynx can display black and 4095 colors. The color table is split up into a green and an blue-red part (so not RGB but GBR).&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
After the ROM has deciphered the boot sector the display memory is at $2000, background color (that is pen 0) is black, all other entries are $f despite entry 14 which is also 0.&lt;br /&gt;
&lt;br /&gt;
So to fill the screen simply do:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
lda #$ff&lt;br /&gt;
ldx #0&lt;br /&gt;
ldy #$20   ; round(160*102/2/256)&lt;br /&gt;
loop:&lt;br /&gt;
    sta $2000,x&lt;br /&gt;
    inx&lt;br /&gt;
  bne loop&lt;br /&gt;
  inc loop+2 ; Self modify&lt;br /&gt;
  dey&lt;br /&gt;
bne loop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Tips 'n' tricks ===&lt;br /&gt;
A nice trick with the new opcodes to limit a counter:&lt;br /&gt;
* 6502&lt;br /&gt;
  lda counter  ; 2&lt;br /&gt;
  inc          ; 1&lt;br /&gt;
  and #3       ; 2&lt;br /&gt;
  sta counter  ; 2 = 7 bytes&lt;br /&gt;
&lt;br /&gt;
* 65C02&lt;br /&gt;
  inc counter ; 2&lt;br /&gt;
  rmb2 counter ; 2 =&amp;gt; 4 bytes&lt;br /&gt;
&lt;br /&gt;
Set a counter (previously 0) to a power-of-two (here 32)&lt;br /&gt;
&lt;br /&gt;
* 6502&lt;br /&gt;
  lda #32     ; 2&lt;br /&gt;
  sta counter ; 2 =&amp;gt; 4&lt;br /&gt;
&lt;br /&gt;
* 65C02&lt;br /&gt;
  smb5 counter ; 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
*[https://www.monlynx.de/lynx/lynxdoc.html Hardware documentation]&lt;br /&gt;
*[https://github.com/42Bastian/lynx_hacking/tree/master/248b Size coding examples.]&lt;br /&gt;
*[https://github.com/42Bastian/lynx-encryption-tools Lynx encryption-tools]&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=998</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=998"/>
				<updated>2022-01-10T21:32:31Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Overwriting return address on stack at $1F8 */ missed high byte for address&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $1F8 (low byte) and $1F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=198357 Everything about illegal opcodes and their use in a C64 system&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html (with an additional presentation at https://www.youtube.com/watch?v=kJ-EXbnw0kQ )&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=840</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=840"/>
				<updated>2021-02-19T18:35:20Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Sync with frame */ $d011 and $d012 was mixed up&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENO  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$35,$39  ;address to jump to in ASCII code: &amp;quot;2059&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
start   lax #0        ;actually address of next BASIC line, $xx00 means end of BASIC program, use $xx for an opcode&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 bytes for your program, coming with A and X initialised to 0. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENO for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following native video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual foreground color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, only 8 foreground colors, can be mixed with hires chars, foreground &amp;lt; 8 denotes a hires char, with foreground &amp;gt;= 8 a pair of two bits (brick pixel) define color: background, foreground &amp;amp; %111, common color 1+2&lt;br /&gt;
* Extended background color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, a pair of two bits (brick pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D012 and the highest bit of register $D011 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
waitsync:&lt;br /&gt;
        ldx $D012&lt;br /&gt;
        inx&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=198357 Everything about illegal opcodes and their use in a C64 system&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=834</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=834"/>
				<updated>2021-02-16T21:07:10Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Putting your program into zeropage overwriting part of CHRGET */ $AE trick&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
Loading data into address $A9 has an interesting side effect - the vector of $AE/$AF defines the address where the loaded bytes are written to. If $AE gets overwritten with, for example, $40, the next byte will be written to $41 (because the vector is incremented after the write). This can be used to split your program into different parts of memory.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=198357 Everything about illegal opcodes and their use in a C64 system&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=833</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=833"/>
				<updated>2021-02-16T20:57:54Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: added link to No More Secrets document (use of illegal 6502 opcodes in C64)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
* No More Secrets https://csdb.dk/release/?id=198357 Everything about illegal opcodes and their use in a C64 system&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=832</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=832"/>
				<updated>2021-02-16T20:55:22Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Overwriting  CHROUT vector at $326-$327 */ warning regarding datasette buffer&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
* The memory area $33C-$3FB is the datasette buffer, thus loading a program into this area won't work when loading from a cassette.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=831</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=831"/>
				<updated>2021-02-16T00:24:36Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Autostart */ zeropage overwriting part of CHRGET method&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
==== Putting your program into zeropage overwriting part of CHRGET ====&lt;br /&gt;
&lt;br /&gt;
CHRGET is a machine code routine for the BASIC interpreter that is placed in the zero page at $73-$8A. By putting your program at $7C and following, the CHRGET routine runs directly into your code.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$7C&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this method, there is no need for a start address vector. The only deadweight in your program is the loading address. However, the zero page is a busy place, if your program extends beyond address $8f (that is it has more than 22 bytes on disk) you have to deal with some parts of your program being overwritten, for example, the status byte in $90 is set after loading.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=830</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=830"/>
				<updated>2021-02-16T00:06:13Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Sync with frame */ added IRQ-tolerant version&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
This works fine as long as we don't miss the line 255 due to an interrupt. If your program has interrupts that are not synced to a rasterline, you can change the condition as follows&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$E3&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bcs waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we have a window between raster line $E4 and $FF to trigger our sync, which we will meet even if a standard Kernal interrupt comes in-between. Of course, our main program now has to burn around 1700 cycles to avoid a retriggering in the same frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=829</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=829"/>
				<updated>2021-02-15T23:26:44Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Video display */ Sync with frame&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&lt;br /&gt;
Depending on the VIC type, there are 262/263 raster lines (NTSC) or 312 raster lines (PAL-B). Therefore, the VIC uses register at $D011 and the highest bit of register $D012 to represent the current raster line. Checking only the lower 8 bits and waiting for them to be 0 would therefore not work because this happens two times per frame. However, any number greater than 64 will only trigger once. Using a value of 255, we would sync to a rasterline at the end of the screen, well below the part where graphics are normally displayed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #$FF&lt;br /&gt;
waitsync:&lt;br /&gt;
        cmp $D012&lt;br /&gt;
        bne waitsync&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As long as there have more than 63 cycles passed since the snyc, the next sync will happen at the next frame.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=828</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=828"/>
				<updated>2021-02-15T22:59:16Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Useful Kernal functions */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=827</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=827"/>
				<updated>2021-02-15T22:17:48Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Autostart */ stack return address method&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting BASIC idle loop vector at $302-$303 ====&lt;br /&gt;
&lt;br /&gt;
After loading the program, the BASIC idle loop will be called. By overwriting the vector at $302/$303 it is possible to autostart your program. Since the memory before $302 can be safely overwritten, the program would be arranged so that $303 is the '''last''' address of your program. If your program requires a loop back to the start, the vector can be used as part of a JMP where the following two bytes are already there, saving one byte in comparison to a loop done with a branch command.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$304 - (end - start)&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
        ; ...&lt;br /&gt;
        jmp     start    ;the address for start lies in $302/$303, thus will be called by the Kernal after loading&lt;br /&gt;
end:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In cases where the vector has a practical use, the overhead in filesize is only the 2 byte loading address. For programs larger than 93 byte keep in mind that addresses before $2A7 are in use by the Kernal.&lt;br /&gt;
&lt;br /&gt;
==== Overwriting return address on stack at $1F8 ====&lt;br /&gt;
&lt;br /&gt;
When the load routine is called, the stack pointer is always at $FA at a stock C64. Thus the return address from the loading process is stored in memory address $F8 (low byte) and $F9 (high byte). During the loading routine, stack usage will clobber the stack below $1F8, but a program loaded to $1F8 and above will be fine and the overwritten address stored at $1F8/$1F9 will be called after loading.&lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$1F8&lt;br /&gt;
        .word start-1   ;adjust for the RTS command pulling the address from stack and increasing it by 1&lt;br /&gt;
start:&lt;br /&gt;
        ;your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For programs larger than 95 byte keep in mind that addresses after $259 are in use by the Kernal, you might overwrite some values here if you don't have disk operations, but better check the memory map beforehand.&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address and the overwritten address at $1F8/$1F9, your program file will have a deadweight of 4 byte when using this method.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does a inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=826</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=826"/>
				<updated>2021-02-15T21:36:16Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Video display */ added video modes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore has the following video modes:&lt;br /&gt;
* Standard text mode, 40x25 characters, 1 individual color per character, 1 common background color&lt;br /&gt;
* Multicolor text mode, 40x25 characters, every two bits (fat pixel) define color: background, char, common color 1+2&lt;br /&gt;
* Extended color text mode, 40x25 characters, 1 individual foreground color per character, 1 out of 4 background colors, only 64 chars&lt;br /&gt;
* Hires bitmap, 320x200 pixels, an 8x8 field can have an individual foreground and an individual background color&lt;br /&gt;
* Multicolor bitmap, 160x200 pixels, every two bits (fat pixel) define color: background, screen color lower nibble, screen color higher nibble, color from color RAM&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does a inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=825</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=825"/>
				<updated>2021-02-15T21:26:51Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: added links to memory map and to ROM disassembly&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does a inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
* Commodore 64 Memory map https://sta.c64.org/cbm64mem.html&lt;br /&gt;
* C64 BASIC &amp;amp; KERNAL ROM Disassembly https://www.pagetable.com/c64ref/c64disasm/&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=824</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=824"/>
				<updated>2021-02-15T21:22:41Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Useful Kernal functions */ added two examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Useful Kernal functions ===&lt;br /&gt;
&lt;br /&gt;
==== Get a pseudo random number ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #1   &lt;br /&gt;
        jsr     $E09A&lt;br /&gt;
        lda     $63&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The sequence of random numbers is always the same after a cold start or reset, the first 20 numbers are:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 4,24,231,4,177,170,193,101,231,18,196,1,75,48,149,36,124,53,65,154&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Increment a zero page counter, return in X register, clear Y register ====&lt;br /&gt;
&lt;br /&gt;
This is part of the string handling function in the BASIC ROM, but the code could be useful for some loop stuff.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        jsr     $B5FF    ;does a inc $23, ldx $23, ldy #$00, rts&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=823</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=823"/>
				<updated>2021-02-15T21:01:45Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Getting something on screen */ fixed indentation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda     #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr     $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=822</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=822"/>
				<updated>2021-02-15T20:56:34Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: added info for screen output in text mode&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&lt;br /&gt;
Output a character:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
        lda #C       ;C must be the char code according to the PETSCII code table&lt;br /&gt;
        jsr $FFD2    ;you can also use the direct address $FC1A, function preserves A,X,Y and enables interrupt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the [https://en.wikipedia.org/wiki/PETSCII#Character_set PETSCII code table] contains also control characters, this method can be also used to clear the screen, move the cursor or set the text color. However, some functions are shorter (and faster) by directly calling the respective KERNAL function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
Clear screen             JSR $E544&lt;br /&gt;
Cursor home position     JSR $E566&lt;br /&gt;
Output newline character JSR $AAD7&lt;br /&gt;
Set text color           LDA #COLOR, STA $286&lt;br /&gt;
Set border color         LDA #COLOR, STA $D020   ;not possible with a PETSCII control code&lt;br /&gt;
Set background color     LDA #COLOR, STA $D021   ;not possible with a PETSCII control code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy characters directly to screen memory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm   ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
LINE=5    ;for example&lt;br /&gt;
COLUMN=10 ;for example&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the text needs to be stored in '''screen code''' format, not in PETSCII (see [https://sta.c64.org/cbm64pettoscr.html Commodore 64 PETSCII code to screen code conversion] for the differences). Furthermore, the example above only writes to the screen memory, not the color RAM, assuming that the color RAM already contains the intended text color. This is the case after issuing a clear screen on most C64 machines, except for some old ROM revisions. To be on the save side you would have to write to color RAM as well, which costs another 5 byte:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
.macpack cbm     ;scrcode macro&lt;br /&gt;
SCREEN_BASE=$400&lt;br /&gt;
COLOR_RAM  =$D800&lt;br /&gt;
LINE       =5    ;for example&lt;br /&gt;
COLUMN     =10   ;for example&lt;br /&gt;
COLOR      =1    ;white&lt;br /&gt;
&lt;br /&gt;
        ldx     #endtext-text-1&lt;br /&gt;
@loop:  lda     text,x&lt;br /&gt;
        sta     SCREEN_BASE+LINE*40+COLUMN,x&lt;br /&gt;
        lda     #COLOR&lt;br /&gt;
        sta     COLOR_RAM  +LINE*40+COLUMN,x&lt;br /&gt;
        dex&lt;br /&gt;
        bpl     @loop&lt;br /&gt;
text:   scrcode &amp;quot;hello world!&amp;quot;&lt;br /&gt;
endtext:&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=821</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=821"/>
				<updated>2021-02-15T20:19:40Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: Added a memory map overview for C64 section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore system consists of a 6502-compatible MOS 6510 processor with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Memory map ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
    0-1          $0-$1      PROCESSOR PORT REGISTERS, not usable as RAM&lt;br /&gt;
   0-255         $0-$FF     ZEROPAGE, mostly used by BASIC interpreter and Kernal&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-600      $200-$258    INPUT BUFFER&lt;br /&gt;
  601-630      $259-$276    FILE SYSTEM DATA&lt;br /&gt;
  631-640      $277-$280    KEYBOARD BUFFER, filled by Kernal ISR upon key presses &lt;br /&gt;
  641-678      $281-$2A6    VARIOUS KERNAL VARIABLES&lt;br /&gt;
  679-767      $2A7-$2FF    Unused (89 byte)&lt;br /&gt;
  768-779      $300-$30B    KERNAL VECTORS&lt;br /&gt;
  780-783      $30C-$30F    REGISTER VALUES A,X,Y,SR for SYS&lt;br /&gt;
  784-819      $310-$333    KERNAL VECTORS&lt;br /&gt;
  820-827      $334-$33B    Unused (8 byte)&lt;br /&gt;
  828-1019     $33C-$3FB    DATASETTE BUFFER&lt;br /&gt;
 1020-1023     $3FC-$3FF    Unused (4 byte)&lt;br /&gt;
 1024-2023     $400-$7E7    SCREEN MEMORY (25 lines times 40 columns)&lt;br /&gt;
 2024-2039     $7E8-$7F7    Unused (16 byte)&lt;br /&gt;
 2040-2047     $7F8-$7FF    SPRITE COSTUME POINTERS&lt;br /&gt;
 2048          $800         must contain a value of 0 so that the BASIC program can be RUN&lt;br /&gt;
 2049-40959    $801-$9FFF   BASIC AREA (code, variables, arrays, strings)&lt;br /&gt;
40960-49151   $A000-$BFFF   BASIC ROM (unused RAM underneath)&lt;br /&gt;
49152-53247   $C000-$CFFF   UPPER RAM AREA (unused)&lt;br /&gt;
53248-53294   $D000-$D02E   VIC-II, memory-mapped graphics chip registers)&lt;br /&gt;
54272-54300   $D400-$D41C   SID, memory-mapped sound chip registers)&lt;br /&gt;
55296-56295   $D800-$DBE7   Color RAM, only lower 4 bits are stored, higher nibble undefined&lt;br /&gt;
56296-56319   $DBE8-$DBFF   Unused (24 half-bytes)&lt;br /&gt;
56320-56335   $DC00-$DC0F   CIA#1, memory-mapped I/O chip registers for keyboard, joystick, mouse, datasette, IRQ control&lt;br /&gt;
56576-56591   $DD00-$DD0F   CIA#1, memory-mapped I/O chip registers for serial bus, RS232, NMI control&lt;br /&gt;
57344-65535   $E000-$FFFF   KERNAL ROM&lt;br /&gt;
65534-65535   $FFFE-$FFFF   Execution address of interrupt service routine, can only be set if KERNAL is turned off&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a detailed explanation of the C64 memory map check out [https://sta.c64.org/cbm64mem.html Commodore 64 memory map] on Joe Forster/STA homepage.&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=820</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=820"/>
				<updated>2021-02-15T19:34:20Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Program file overhead */ wording&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be chosen between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=819</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=819"/>
				<updated>2021-02-15T19:31:45Z</updated>
		
		<summary type="html">&lt;p&gt;Wil: /* Autoboot */ added info on program file overhead and on autorun via $326/7&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a 6502 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
[[File:6502.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== The 6502 processor  ===&lt;br /&gt;
The 6502 processor can be seen as the 8-bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumulator, X and Y registers) and a handful of instructions to work with.&lt;br /&gt;
&lt;br /&gt;
=== Adressing modes ===&lt;br /&gt;
To be added.&lt;br /&gt;
&lt;br /&gt;
=== Zero page ===&lt;br /&gt;
When using the 6502 for sizecoding, you'll mostly be working from zeropage&lt;br /&gt;
&lt;br /&gt;
=== General 6502 Resources ===&lt;br /&gt;
* 6502.org http://www.6502.org/&lt;br /&gt;
* 6502 instruction reference http://www.6502.org/tutorials/6502opcodes.html&lt;br /&gt;
* 6502 books http://retro.hansotten.nl/6502-sbc/&lt;br /&gt;
* 6502 Assembler tutorial https://dwheeler.com/6502/oneelkruns/asm1step.html&lt;br /&gt;
* Easy 6502 code tester https://skilldrick.github.io/easy6502/&lt;br /&gt;
* Synthetic instructions https://wiki.nesdev.com/w/index.php/Synthetic_instructions#8-bit_rotate&lt;br /&gt;
&lt;br /&gt;
== Atari 8-bit family ==&lt;br /&gt;
The systems in this family are: Atari 400, 800, 5200, 1200XL, 600XL, 800XL, 130XE, 65XE, 800XE and XEGS.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Atari 8-bit systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari 8bit systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: MADS Assembler - This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://mads.atari8.info/&lt;br /&gt;
* Emulator(s): I Found Altirra to work best for my usecase. Make sure to use the original Rev2 rom for best compatibility.&lt;br /&gt;
&lt;br /&gt;
==== Special Memory Adresses ====&lt;br /&gt;
* FRAMECOUNTER_HIGH = 19&lt;br /&gt;
* FRAMECOUNTER_LOW  = 20&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Atari 8bit systems use the ANTIC and GTIA chips. Information can be found here:&lt;br /&gt;
* https://en.wikipedia.org/wiki/ANTIC&lt;br /&gt;
* https://www.atariarchives.org/agagd/chapter1.php&lt;br /&gt;
&lt;br /&gt;
==== Sync with frame  ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
      lda RTCLOK+2&lt;br /&gt;
waits&lt;br /&gt;
      cmp RTCLOK+2&lt;br /&gt;
      beq waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you don't mind trashing RTCLOK&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
RTCLOK      equ $0012&lt;br /&gt;
waits&lt;br /&gt;
      lsr RTCLOK+2&lt;br /&gt;
      bcc waits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which is two bytes shorter.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
;fill screen with charset,(88,89)=an address&lt;br /&gt;
 org $600; free 6th page:600-6ff&lt;br /&gt;
 ldy #0&lt;br /&gt;
fl: tya&lt;br /&gt;
 sta(88),y&lt;br /&gt;
 iny&lt;br /&gt;
 bne fl&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
SDMCTL	= $022f&lt;br /&gt;
HPOSP0  = $d000&lt;br /&gt;
SIZEP0  = $d008&lt;br /&gt;
GRAFP0  = $d00d&lt;br /&gt;
COLPM0  = $d012&lt;br /&gt;
&lt;br /&gt;
FRAMECOUNTER_HIGH = 19&lt;br /&gt;
FRAMECOUNTER = 20&lt;br /&gt;
WSYNC	= $d40a&lt;br /&gt;
VCOUNT	= $d40b&lt;br /&gt;
&lt;br /&gt;
sinewave	= $0600		; to $06ff&lt;br /&gt;
&lt;br /&gt;
		org $80&lt;br /&gt;
&lt;br /&gt;
main	&lt;br /&gt;
	; disable all graphics/colors&lt;br /&gt;
	ldx #0&lt;br /&gt;
	stx SDMCTL	&lt;br /&gt;
&lt;br /&gt;
	ldy #$7f&lt;br /&gt;
	sty SIZEP0	; size p0=127&lt;br /&gt;
		&lt;br /&gt;
	ldx #0&lt;br /&gt;
	ldy #$3f&lt;br /&gt;
make_sine:&lt;br /&gt;
value_lo&lt;br /&gt;
			lda #0&lt;br /&gt;
			clc&lt;br /&gt;
delta_lo&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_lo+1&lt;br /&gt;
value_hi&lt;br /&gt;
			lda #0&lt;br /&gt;
delta_hi&lt;br /&gt;
			adc #0&lt;br /&gt;
			sta value_hi+1&lt;br /&gt;
 &lt;br /&gt;
			sta sinewave+$c0,x&lt;br /&gt;
			sta sinewave+$80,y&lt;br /&gt;
			eor #$7f&lt;br /&gt;
			sta sinewave+$40,x&lt;br /&gt;
			sta sinewave+$00,y&lt;br /&gt;
 &lt;br /&gt;
			lda delta_lo+1&lt;br /&gt;
			adc #8&lt;br /&gt;
			sta delta_lo+1&lt;br /&gt;
			bcc nothing&lt;br /&gt;
			inc delta_hi+1&lt;br /&gt;
nothing&lt;br /&gt;
			inx&lt;br /&gt;
			dey&lt;br /&gt;
			bpl make_sine&lt;br /&gt;
&lt;br /&gt;
updateloop:&lt;br /&gt;
		; vblank&lt;br /&gt;
		lda VCOUNT&lt;br /&gt;
		bne updateloop&lt;br /&gt;
&lt;br /&gt;
		; clear graphics&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		ldy #0&lt;br /&gt;
		lda #47&lt;br /&gt;
		sta COLPM0&lt;br /&gt;
yloop:&lt;br /&gt;
		tya           ; graphics shape = y&lt;br /&gt;
		sta WSYNC&lt;br /&gt;
		sta GRAFP0&lt;br /&gt;
&lt;br /&gt;
		; a = sin(frame+y)+48&lt;br /&gt;
		tya	&lt;br /&gt;
		adc FRAMECOUNTER&lt;br /&gt;
		tax&lt;br /&gt;
		lda sinewave,x&lt;br /&gt;
		adc #48&lt;br /&gt;
		sta HPOSP0&lt;br /&gt;
                &lt;br /&gt;
                iny&lt;br /&gt;
                bne yloop&lt;br /&gt;
		jmp updateloop&lt;br /&gt;
&lt;br /&gt;
		run main&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Atari 8-bit use the POKEY chip to generate sound.&lt;br /&gt;
* https://en.wikipedia.org/wiki/POKEY&lt;br /&gt;
&lt;br /&gt;
==== BASIC ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
; from Analog, 1985-09,pp.25-32&lt;br /&gt;
*=$2000&lt;br /&gt;
AUDF1=$D200&lt;br /&gt;
AUDC1=$D201&lt;br /&gt;
AUDCTL=$D208&lt;br /&gt;
SKCTL=$D20F&lt;br /&gt;
&lt;br /&gt;
.MACRO SOUND ; voice,pitch,dist,vol;,dur&lt;br /&gt;
	lda #%2&lt;br /&gt;
	sta AUDF1+2*%1&lt;br /&gt;
    lda #[[%3 * 16] | %4] ;lda #[[%3 shl 4] or %4]&lt;br /&gt;
    sta AUDC1+2*%1&lt;br /&gt;
.ENDM&lt;br /&gt;
&lt;br /&gt;
 lda #0&lt;br /&gt;
 sta AUDCTL&lt;br /&gt;
 lda #3&lt;br /&gt;
 sta SKCTL&lt;br /&gt;
&lt;br /&gt;
 SOUND 0,121,10,8&lt;br /&gt;
&lt;br /&gt;
 jmp *&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are:&lt;br /&gt;
* Mapping the Atari https://www.atariarchives.org/mapping/&lt;br /&gt;
* Atari 8bit Memory map https://www.atariarchives.org/mapping/memorymap.php&lt;br /&gt;
* Fready's undocumented 6502 opcodes https://github.com/FreddyOffenga/6502&lt;br /&gt;
* Atari OS Rev2 disassembly for MADS assembler https://github.com/ilmenit/A800-OS-XL-Rev2&lt;br /&gt;
* Fready's github https://github.com/FreddyOffenga/&lt;br /&gt;
&lt;br /&gt;
== Commodore 64 ==&lt;br /&gt;
The Commodore systems consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Commodore systems is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: To be added&lt;br /&gt;
* Emulator(s): VICE is the way to go&lt;br /&gt;
&lt;br /&gt;
=== Program file overhead ===&lt;br /&gt;
&lt;br /&gt;
A standard CBM file consists of a 2 byte loading address followed by the data that is placed into memory starting at this address. In addition, if your machine program should be able to be started with &amp;quot;RUN&amp;quot;, you need a BASIC stub that is loaded into memory from $0801 to $080c, typically followed by your machine program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0801&lt;br /&gt;
        .word $080b   ;address of next BASIC line&lt;br /&gt;
        .word LINENR  ;line number, can be set between 0 and 63999&lt;br /&gt;
        .byte $9e     ;token for SYS command&lt;br /&gt;
        .byte $32,$30,$36,$31  ;address to jump to in ASCII code: &amp;quot;2061&amp;quot; &lt;br /&gt;
        .byte $00     ;end of BASIC line&lt;br /&gt;
        .word $0000   ;address of next BASIC line, $0000 means end of BASIC program  &lt;br /&gt;
start&lt;br /&gt;
; your code here&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the 2 byte load address, this makes an overhead of 14 byte for your program. To reduce this, you can&lt;br /&gt;
* omit the BASIC line and tell users to start your program by directly entering &amp;quot;SYS ''address''&amp;quot;&lt;br /&gt;
* save your program from $0802 on, the byte at $0801 is recovered automatically after loading by the relink function. This reduces your program file size by 1 byte&lt;br /&gt;
* accept the loss and use the 2 byte establishing LINENR for something good, they are copied to zeropage addresses $39/$3a automatically, saving you the effort of initializing the addresses&lt;br /&gt;
* use an autostart method (see below)&lt;br /&gt;
&lt;br /&gt;
Some compos also specify that the BASIC loader does not count towards the overall filesize, check the rules before you worry.&lt;br /&gt;
&lt;br /&gt;
=== Autostart ===&lt;br /&gt;
&lt;br /&gt;
Unlike the Commodore 128, the Commodore 64 has no dedicated built-in auto-boot feature, but it is possible to autorun a program after loading by specifying the load address so that it overwrites a vector or address that is called by the operating system. There are multiple possibilities:&lt;br /&gt;
&lt;br /&gt;
==== Overwriting  CHROUT vector at $326-$327 ====&lt;br /&gt;
&lt;br /&gt;
The vector at address $326-$327 points at the CHROUT routine, normally at address $F1CA.&lt;br /&gt;
If you put the entry address of your program into $326/27, your code will be called when the operating system wants to print &amp;quot;READY.&amp;quot; after loading.&lt;br /&gt;
Note that the vector at address $328-$329 points at the STOP routine that checks the status of Stop key indicator. Since this routine is called multiple times before your code is done loading, its default value $F6ED needs to be preserved. &lt;br /&gt;
&lt;br /&gt;
Application example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
*=$0326&lt;br /&gt;
        .word start              &lt;br /&gt;
        .byte $ed,$f6&lt;br /&gt;
start&lt;br /&gt;
; rest of code&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Together with the two byte load address, the overwritten CHROUT vector and the preserved STOP vector, your program file will have a deadweight of 6 byte: $26 $03 $0a $03 $ed $f6, less than half of the standard version with a BASIC stub with SYS&lt;br /&gt;
&lt;br /&gt;
Caveat:&lt;br /&gt;
* If your program after $32a is longer than 214 byte, part of your program will load into the screen at $400. Thus, issuing a clear screen will overwrite your program. It is possible to move the screen memory somewhere else, but that requires storing a new value into address $D018, which when done with an LDA # / STA pair will cost you 5 byte&lt;br /&gt;
* Since you changed the CHROUT pointer, standard KERNAL functions for printing won't work. For output of single characters, you can still use the direct address $F1CA. Alternatively, you can restore the vectors by calling $FD15, which will overwrite the memory $314 to $333 with the default values.&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the Commodore, it has the following video modes:&lt;br /&gt;
&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The Commodore 64 uses the famous SID chip to generate sound.&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Codebase 64 https://codebase64.org/doku.php?id=base:start&lt;br /&gt;
&lt;br /&gt;
== Apple II ==&lt;br /&gt;
The Apple II is an 8-bit home computer and one of the world's first highly successful mass-produced microcomputer products. It was designed primarily by Steve Wozniak.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
* ACME 6502 cross-assembler(https://sourceforge.net/projects/acme-crossass/)&lt;br /&gt;
* Apple Commander (http://applecommander.sourceforge.net) for batch compilation&lt;br /&gt;
* AppleWin emulator (https://github.com/AppleWin/AppleWin/releases). Supports Mockingboard card(AY-8910+speech synthesier), HDD, Z80 card(for CP/M), mouse etc.&lt;br /&gt;
* CiderPress(https://a2ciderpress.com)&lt;br /&gt;
&lt;br /&gt;
Compilation can be done as follows (master.dsk can be found with applewin) &lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
acme hl.asm&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -d master.dsk hl&lt;br /&gt;
java -jar AppleCommander-1.3.5.jar -p master.dsk hl B 24576 &amp;lt; hl.bin&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Map ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
   0-255	 $0-$FF     ZERO-PAGE SYSTEM STORAGE&lt;br /&gt;
  256-511      $100-$1FF    SYSTEM STACK&lt;br /&gt;
  512-767      $200-$2FF    KEYBOARD CHARACTER BUFFER&lt;br /&gt;
  768-975      $300-$3CF    OFTEN AVAILABLE AS FREE SPACE FOR USER PROGRAMS&lt;br /&gt;
  976-1023     $3D0-3FF     SYSTEM VECTORS&lt;br /&gt;
 1024-2047     $400-$7FF    TEXT AND LO-RES GRAPHICS PAGE 1 &amp;lt;--- !!!&lt;br /&gt;
 2048-LOMEM    $800-LOMEM   PROGRAM STORAGE&lt;br /&gt;
 2048-3071     $800-$BFF    TEXT AND LO-RES GRAPHICS PAGE 2 OR FREE SPACE&lt;br /&gt;
 3072-8191     $C00-$1FFF   FREE SPACE UNLESS RAM APPLESOFT IS IN USE&lt;br /&gt;
 8192-16383   $2000-$3FFF   HI-RES PAGE 1 OR FREE SPACE &amp;lt;--- !!!&lt;br /&gt;
16384-24575   $4000-$5FFF   HI-RES PAGE 2 OR FREE SPACE&lt;br /&gt;
24576-38999   $6000-$95FF   FREE SPACE AND STRING STORAGE&lt;br /&gt;
38400-49151   $9600-$BFFF   DOS&lt;br /&gt;
49152-53247   $C000-$CFFF   I/O HARDWARE (RESERVED)&lt;br /&gt;
53248-57343   $D000-$DFFF   APPLESOFT IN LANGUAGE CARD OR ROM&lt;br /&gt;
57344-63487   $E000-$F7FF   APPLESOFT OR INTEGER BASIC IN LANGUAGE CARD OR ROM&lt;br /&gt;
63488-65535   $F800-$FFFF   SYSTEM MONITOR&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Display ===&lt;br /&gt;
&lt;br /&gt;
=== Graphics Modes ===&lt;br /&gt;
: Text Mode 40x24, for Apple IIe available 80x25 - use PR#3 for switch mode, or hardware switch&lt;br /&gt;
: LowRes 40x48, 16 colors: https://en.wikipedia.org/wiki/Apple_II_graphics &lt;br /&gt;
: Hires mode 280x192,6 colors: https://www.xtof.info/blog/?p=768&lt;br /&gt;
https://mrob.com/pub/xapple2/colors.html&lt;br /&gt;
https://archive.org/details/HiRes_Color_Graphics_on_the_Apple_II_Computer_by_Wozniak&lt;br /&gt;
&lt;br /&gt;
However for sizecoding, you almost never want to do direct-access to graphics for Apple II in size-coding because the Apple II graphics modes are horrible.  The only fast way to do things is with large lookup tables.  To do hires you need to divide by 7 which as you can imagine is a bit difficult to do compactly on 6502. Double-hires is even crazier on top of that.  Deater did manage a color-bar style effect in double-hires in 128B but that was doing some crazy tricks with the firmware BASIC routines, definitely not direct-access.&lt;br /&gt;
&lt;br /&gt;
Lores and Hires can be mixed modes and full-graphics&lt;br /&gt;
The screen structure is called memory holes(https://retrocomputing.stackexchange.com/questions/2534/what-are-the-screen-holes-in-apple-ii-graphics). The GBASCALC($F847) procedure is used to calculate the address of the horizontal line : IN:reg.A=Y, out : GBASL/GBASH($26/$27)=address. See also https://www.callapple.org/uncategorized/use-of-apple-ii-color-graphics-in-assembly-language/&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Here is an example of a XOR texture, created by g0blinish&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
GBASL	=	$26&lt;br /&gt;
GBASH	=	$27&lt;br /&gt;
SETGR    =     $FB40 ; setup LoRes&lt;br /&gt;
GBASCALC = $F847 ; calc Address&lt;br /&gt;
&lt;br /&gt;
CLRTEXT =  $C050 ;display graphics &lt;br /&gt;
SETTEXT =  $C051 ;display text &lt;br /&gt;
&lt;br /&gt;
CLRMIXED = $C052 ;clear mixed mode- enable full graphics &lt;br /&gt;
SETMIXED = $C053 ;enable graphics/text mixed mode &lt;br /&gt;
&lt;br /&gt;
PAGE1 =    $C054 ;select text/graphics page1 &lt;br /&gt;
PAGE2 =    $C055 ;select text/graphics page2 &lt;br /&gt;
&lt;br /&gt;
CLRHIRES = $C056 ;select Lo-res &lt;br /&gt;
SETHIRES = $C057 ;select Hi-res &lt;br /&gt;
&lt;br /&gt;
TMP= $FA&lt;br /&gt;
&lt;br /&gt;
	JSR   SETGR      ;GR&lt;br /&gt;
	BIT CLRMIXED ; full screen&lt;br /&gt;
&lt;br /&gt;
	LDA #0 ; A=0&lt;br /&gt;
	STA TMP ; POKE $FA,A&lt;br /&gt;
&lt;br /&gt;
YLP ;&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
;	LSR ; A=A/2&lt;br /&gt;
	JSR GBASCALC&lt;br /&gt;
	LDY #0;Y=0&lt;br /&gt;
&lt;br /&gt;
XLP TYA ; A=Y&lt;br /&gt;
	EOR TMP ; A=A xor PEEK($FA)&lt;br /&gt;
	and #$0F ; A=A and 15&lt;br /&gt;
	TAX ; X=A&lt;br /&gt;
	LDA COLORS,X ;A=PEEK(COLORS+X)&lt;br /&gt;
	STA(GBASL),Y ; POKE PEEK($26)+256*PEEK($27)+Y,A&lt;br /&gt;
	INY ; Y=Y+1&lt;br /&gt;
	CPY #40 ; Y=40?&lt;br /&gt;
	BNE XLP&lt;br /&gt;
	INC TMP ; POKE $FA,PEEK($FA)+1&lt;br /&gt;
	LDA TMP ; A=PEEK($FA)&lt;br /&gt;
	CMP #24 ; A=24?&lt;br /&gt;
	BNE YLP&lt;br /&gt;
	&lt;br /&gt;
M1 JMP M1 ; replace to RTS&lt;br /&gt;
&lt;br /&gt;
COLORS ;N*17, pixel format is AAAABBBB, AAAA - upper dot, BBBB - lower dot&lt;br /&gt;
!byte $00,$11,$22,$33,$44,$55,$66,$77&lt;br /&gt;
!byte $88,$99,$AA,$BB,$CC,$DD,$EE,$FF&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Sound  ====&lt;br /&gt;
Here is an example for using the speaker, based onthe following basic program:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;basic&amp;quot;&amp;gt;&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;6502&amp;quot;&amp;gt;&lt;br /&gt;
 *=$6000&lt;br /&gt;
 !to &amp;quot;HL.bin&amp;quot;, plain	; set output file and format&lt;br /&gt;
 !cpu 6502		; set processor type&lt;br /&gt;
&lt;br /&gt;
;start&lt;br /&gt;
; 95  FOR K = 1 TO N: READ V(K),P(K): NEXT K&lt;br /&gt;
; 100  FOR K = 1 TO N:V = V(K):P = P(K)&lt;br /&gt;
ini:&lt;br /&gt;
	lda #70&lt;br /&gt;
	sta cnt+1&lt;br /&gt;
	lda #music&amp;amp;255&lt;br /&gt;
	sta gotbyte+1&lt;br /&gt;
	lda #music/256&lt;br /&gt;
	sta gotbyte+2&lt;br /&gt;
&lt;br /&gt;
lop:&lt;br /&gt;
;V&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L300&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
;P&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	sta L301&lt;br /&gt;
	jsr gotbyte&lt;br /&gt;
	clc&lt;br /&gt;
	adc #1&lt;br /&gt;
	sta L320&lt;br /&gt;
	jsr beep&lt;br /&gt;
	&lt;br /&gt;
	dec cnt+1&lt;br /&gt;
cnt lda #70&lt;br /&gt;
	bne lop&lt;br /&gt;
; 110  GOSUB 50&lt;br /&gt;
; 50  POKE 768,V: POKE 769,P - 255 *  INT (P / 256): POKE 800,1 + P / 256&lt;br /&gt;
; 60  CALL 770: RETURN &lt;br /&gt;
	jmp ini&lt;br /&gt;
gotbyte&lt;br /&gt;
	lda music&lt;br /&gt;
	inc gotbyte+1&lt;br /&gt;
	bne noinch&lt;br /&gt;
	inc gotbyte+2&lt;br /&gt;
noinch&lt;br /&gt;
	rts&lt;br /&gt;
;!byte 173,48,192,136,208,5,206,1,3,240,9,202,208,245,174,0,3,76,2,3,206,32,3,208,240,96&lt;br /&gt;
beep:&lt;br /&gt;
	ldy #1&lt;br /&gt;
	ldx #1&lt;br /&gt;
loc_302:&lt;br /&gt;
		LDA	$C030&lt;br /&gt;
&lt;br /&gt;
loc_305:&lt;br /&gt;
		DEY&lt;br /&gt;
		BNE	loc_30D&lt;br /&gt;
		DEC	L301&lt;br /&gt;
loc_30B:&lt;br /&gt;
		BEQ	loc_316&lt;br /&gt;
&lt;br /&gt;
loc_30D:&lt;br /&gt;
		DEX&lt;br /&gt;
		BNE	loc_305&lt;br /&gt;
		LDX	L300&lt;br /&gt;
		JMP	loc_302&lt;br /&gt;
loc_316:&lt;br /&gt;
		DEC	L320&lt;br /&gt;
		BNE	loc_30B&lt;br /&gt;
		RTS&lt;br /&gt;
L301 !byte 0&lt;br /&gt;
L300 !byte 0&lt;br /&gt;
L320 !byte 0&lt;br /&gt;
music&lt;br /&gt;
 !word 76,192,85,64,96,64,102,64,114,128,114,64,96,64,102,64,114,64,128,64&lt;br /&gt;
 !word 114,64,152,64,171,64,152,512,76,192,85,64,96,64,102,64,114,128,114,64&lt;br /&gt;
 !word 96,64,102,64,114,64,128,64,114,64,152,64,171,64,152,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,128,114,64,128,64,114,512,85,64,85,64&lt;br /&gt;
 !word 85,64,96,64,144,128,144,64,128,64,76,128,85,64,96,64,144,128,114,64&lt;br /&gt;
 !word 96,64,102,128,114,64,128,64,128,64,128,128,96,64,85,64,96,64,102,64,114,64,114,64&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
* Deater's page on Apple II sizecoding http://www.deater.net/weave/vmwprod/demos/sizecoding.html&lt;br /&gt;
* Article on double hi-res http://www.battlestations.zone/2017/04/apple-ii-double-hi-res-from-ground-up.html&lt;br /&gt;
* Applesoft Hi-Res Subroutines : http://hackzapple.org/scripts_php/index.php?menu=5&amp;amp;mod=ASM&amp;amp;sub=AAL&amp;amp;sub2=8112&amp;amp;PHPSESSID=f65fabfd0cdbf56b6bdc0ddac25117c6#a2&lt;br /&gt;
&lt;br /&gt;
== Atari Lynx ==&lt;br /&gt;
The Atari Lynx consists of the 6502 with custom hardware for graphics and sound.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the Atari Lynx:&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise ====&lt;br /&gt;
To be added soon.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari Lynx are sparse&lt;br /&gt;
* 42Bastian's website (link to be added)&lt;/div&gt;</summary>
		<author><name>Wil</name></author>	</entry>

	</feed>