<?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=G0blinish</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=G0blinish"/>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/wiki/Special:Contributions/G0blinish"/>
		<updated>2026-05-02T04:55:50Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.0</generator>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=817</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=817"/>
				<updated>2021-02-08T17:05:43Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: &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;
==== 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;
=== Autoboot ===&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;
Will give you autoboot and more space directly. (though writing through to $0400 will load it onto the screen unless you move the pointers)&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>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=761</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=761"/>
				<updated>2020-12-30T05:05:35Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: /* Getting something on screen */&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 8bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumilator, IX and IY registers) and only 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;
== Atari 8bit family ==&lt;br /&gt;
The Atari XE/XL 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 TIA chip, 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;
&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 8bit systems use the Pokey 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;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are sparse&lt;br /&gt;
* Fready's github 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;
&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;
=== Autoboot ===&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;
Will give you autoboot and more space directly. (though writing through to $0400 will load it onto the screen unless you move the pointers)&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;
* links to be added&lt;br /&gt;
&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>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Z80&amp;diff=760</id>
		<title>Z80</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Z80&amp;diff=760"/>
				<updated>2020-12-24T05:31:51Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: /* Useful routines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a Z80 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== Registers ===&lt;br /&gt;
The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities.&lt;br /&gt;
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.&lt;br /&gt;
These are the register pairs of the Z80, as seen from a X86 programmers perspective.&lt;br /&gt;
&lt;br /&gt;
* AF = AL + Flags&lt;br /&gt;
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.&lt;br /&gt;
* BC = Can be seen as CX (B=CH,C=CL), often used for loops &lt;br /&gt;
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting&lt;br /&gt;
* IX = 16 bit Index Register X, can also be accessed with IXH,IXL &lt;br /&gt;
* IY = 16 bit Index Register Y, can also be accessed with IYH,IYL&lt;br /&gt;
&lt;br /&gt;
For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:&lt;br /&gt;
* EX AF,AF' = Swaps AF with AF'&lt;br /&gt;
* EXX = Swaps BC, DE and HL with BC', DE' and HL' &lt;br /&gt;
There are no shadow registers for the index registers.&lt;br /&gt;
&lt;br /&gt;
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers. &lt;br /&gt;
The Sjasmplus assembler doesn't really do proper syntax checking for this so beware.&lt;br /&gt;
&lt;br /&gt;
=== Instructions ===&lt;br /&gt;
Here is a rough translation for some of the Z80 instructions:&lt;br /&gt;
&lt;br /&gt;
* BIT = TEST&lt;br /&gt;
* CP = CMP  (although the Z80 has many other handy compare functionality)&lt;br /&gt;
* DJNZ = LOOP (decreases B and checks not zero)&lt;br /&gt;
* EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA&lt;br /&gt;
* EX = XCHG&lt;br /&gt;
* HALT = HLT&lt;br /&gt;
* JP = JMP&lt;br /&gt;
* JR = JMP NEAR (Jump Relative)&lt;br /&gt;
* LD = MOV&lt;br /&gt;
* LDI = MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++)&lt;br /&gt;
* LDIR = REP MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Learning Z80 Assembler ===&lt;br /&gt;
There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-)  &lt;br /&gt;
&lt;br /&gt;
* http://www.z80.info/lesson1.htm&lt;br /&gt;
* http://www.z80.info/lesson2.htm&lt;br /&gt;
* http://www.z80.info/lesson3.htm&lt;br /&gt;
* http://www.z80.info/lesson4.htm&lt;br /&gt;
* http://www.z80.info/lesson5.htm&lt;br /&gt;
&lt;br /&gt;
There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.&lt;br /&gt;
&lt;br /&gt;
Also, here is a compact 'cheat sheet' with some basics for various Z80 systems:&lt;br /&gt;
https://www.chibiakumas.com/z80/CheatSheet.pdf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ZX Spectrum  ==&lt;br /&gt;
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: SJASMPLUS -This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://sourceforge.net/projects/sjasmplus/&lt;br /&gt;
* Pasmo - for the .TAP create. Available at : http://pasmo.speccy.org&lt;br /&gt;
* Emulator(s): I Found [https://www.zophar.net/sinclair/zx-spin.html ZX Sping], [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.&lt;br /&gt;
&lt;br /&gt;
=== Start values ===&lt;br /&gt;
Upon startup (when called from basic), the following values can assumed:&lt;br /&gt;
&lt;br /&gt;
* The alternate HL register is set to 0x2758&lt;br /&gt;
* BC = start address&lt;br /&gt;
* A = C&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory. &lt;br /&gt;
It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.&lt;br /&gt;
&lt;br /&gt;
Address = 010RRLLL RRRCCCCC &lt;br /&gt;
&lt;br /&gt;
* where RRRRR is the row number (0..23)&lt;br /&gt;
* CCCCC is the column number (0..31)&lt;br /&gt;
* LLL is the line number within the cell (0..7)&lt;br /&gt;
&lt;br /&gt;
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout&lt;br /&gt;
&lt;br /&gt;
==== Adding Color ====&lt;br /&gt;
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile. &lt;br /&gt;
It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.&lt;br /&gt;
&lt;br /&gt;
color = brightness(64) | (PAPER&amp;lt;&amp;lt;3) | INK&lt;br /&gt;
&lt;br /&gt;
Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.&lt;br /&gt;
&lt;br /&gt;
==== Border Color ====&lt;br /&gt;
You can set the border color to any of the 8 colors with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld a,0 ; bottom three bits of a contain the color&lt;br /&gt;
out (254),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Configuration ===&lt;br /&gt;
128K separated to 8 pages (16384 bytes size)&lt;br /&gt;
Default configuration is:&lt;br /&gt;
&lt;br /&gt;
: page 5: $4000-$7fff&lt;br /&gt;
: page 2: $8000-$Bfff&lt;br /&gt;
: page 0: $C000-$Ffff&lt;br /&gt;
&lt;br /&gt;
There are two screens - page 7 and page 5.&lt;br /&gt;
port $7FFD allow to control memory and screens:&lt;br /&gt;
: bits 0-2 - page number mapped to memory at $C0000&lt;br /&gt;
: bit 3 - Select page 5(0) or page 7(1) to be displayed.&lt;br /&gt;
: bit 4 - ROM Select. 0-128K,1-48K&lt;br /&gt;
&lt;br /&gt;
Example of use&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
loop: ei&lt;br /&gt;
 halt&lt;br /&gt;
pg: ld a,$17&lt;br /&gt;
 ld bc,$7ffd&lt;br /&gt;
 out (c),a&lt;br /&gt;
 xor $0A&lt;br /&gt;
 ld (pg+1),a&lt;br /&gt;
... do something with $C000-$DB00&lt;br /&gt;
 jp loop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$17 is use 48K, map page 7&lt;br /&gt;
After xor $0A we'll get value $1D(use 48K ROM, display screen at page 7 and map page5 at $C000).This is so-called &amp;quot;double buffering&amp;quot;.&lt;br /&gt;
See also : https://worldofspectrum.org/faq/reference/128kreference.htm&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
 ld de,5800h&lt;br /&gt;
 ld b,24&lt;br /&gt;
yloop:&lt;br /&gt;
   ld c,32&lt;br /&gt;
xloop:&lt;br /&gt;
   ld a,c&lt;br /&gt;
   and b&lt;br /&gt;
   and 7    ; make sure range is 0..7&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
   jr nz,xloop&lt;br /&gt;
djnz yloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using a Backbuffer ====&lt;br /&gt;
While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
halt               ; synchronize &lt;br /&gt;
ld hl,BACKBUFFER   &lt;br /&gt;
ld de,5800h&lt;br /&gt;
ld bc,768&lt;br /&gt;
ldir&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
	; main loop starts here&lt;br /&gt;
	ld	a,00010111b	; set up memory banks and screen here&lt;br /&gt;
mainloop&lt;br /&gt;
	halt			; sync&lt;br /&gt;
	xor	00001010b	; flip screens&lt;br /&gt;
	out	($fd),a&lt;br /&gt;
	push	af&lt;br /&gt;
&lt;br /&gt;
	; render code goes here&lt;br /&gt;
	; screen buffer is location at 0xC000 instead of 0x4000&lt;br /&gt;
&lt;br /&gt;
	pop	af&lt;br /&gt;
	jr	mainloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Overlaying simple graphics ====&lt;br /&gt;
If you don't want to use a solid color/tile for. &lt;br /&gt;
You could copy a single tile across the screen at startup for some flair.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
   ld a,0x55   ; 01010101 pattern&lt;br /&gt;
   ld bc,0x1800&lt;br /&gt;
copyloop:&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
jr nz,copyloop&lt;br /&gt;
djnz copyloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively you could generate a pattern using logic operations or random noise/data.&lt;br /&gt;
==== Useful routines ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;calculate address of next line,HL=address&lt;br /&gt;
down_hl:&lt;br /&gt;
 INC h&lt;br /&gt;
 LD A,h&lt;br /&gt;
 AND 7&lt;br /&gt;
 RET NZ&lt;br /&gt;
 LD A,L&lt;br /&gt;
 ADD A,#20&lt;br /&gt;
 LD L,A&lt;br /&gt;
 RET C&lt;br /&gt;
 LD A,H&lt;br /&gt;
 SUB 8&lt;br /&gt;
 LD H,A&lt;br /&gt;
 RET&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;move up at screen, HL=address&lt;br /&gt;
up_hl&lt;br /&gt;
     LD A,H&lt;br /&gt;
     DEC H&lt;br /&gt;
     AND 7&lt;br /&gt;
     ret nz&lt;br /&gt;
     LD A,L&lt;br /&gt;
     SUB 32&lt;br /&gt;
     LD L,A&lt;br /&gt;
     ret c&lt;br /&gt;
     LD A,H&lt;br /&gt;
     ADD A,8&lt;br /&gt;
     LD H,A&lt;br /&gt;
 ret&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;write pixel&lt;br /&gt;
 CALL  8933 ; C=X(0..255),B=Y(0..175),5C7D=COORDS&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;Calculate vertical line screen Address, IN : A=Y coordinate, OUT : HL=address&lt;br /&gt;
py2saddr:&lt;br /&gt;
        ld l,a&lt;br /&gt;
;	ld a,l&lt;br /&gt;
	and $07&lt;br /&gt;
	ld h,a&lt;br /&gt;
	ld a,l&lt;br /&gt;
	and $c0&lt;br /&gt;
	rra&lt;br /&gt;
	inc a&lt;br /&gt;
	rrca&lt;br /&gt;
	rrca&lt;br /&gt;
	or h&lt;br /&gt;
	ld h,a&lt;br /&gt;
	ld a,l&lt;br /&gt;
	add a&lt;br /&gt;
	add a&lt;br /&gt;
	and $e0&lt;br /&gt;
	ld l,a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;Calculate Attribute address, IB: reg D=Y,reg E=X, OUT: HL=address, destroys DE&lt;br /&gt;
 ld l,d&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 ld h,$11&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 ld d,0&lt;br /&gt;
 add hl,de&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or alter version&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;H=Y coordinate,L=X coordinate&lt;br /&gt;
	ld a,h&lt;br /&gt;
	rrca&lt;br /&gt;
	rrca&lt;br /&gt;
	rrca&lt;br /&gt;
	push af&lt;br /&gt;
	and 3&lt;br /&gt;
	add a,$58&lt;br /&gt;
	ld h,a&lt;br /&gt;
	pop af&lt;br /&gt;
	and %11100000&lt;br /&gt;
	add a,l&lt;br /&gt;
	ld l,a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
calculate Pixel coordinate&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;IN: A=X coordinate,C=L=Y coordinate&lt;br /&gt;
;OUT: HL=screen address,A=bit value&lt;br /&gt;
call #22B0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker.&lt;br /&gt;
Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - Beeper ====&lt;br /&gt;
Setting/toggling bit 4 of port 254 will enable the beeper speaker.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
		ld a,$10	;Bit 4 set&lt;br /&gt;
beeploop:	xor $10		;toggle Bit 4&lt;br /&gt;
		out (254),a&lt;br /&gt;
		ld b,90		;wait&lt;br /&gt;
		djnz $&lt;br /&gt;
		jp beeploop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that the bottom three bits of port 254 also set the border color (see above).&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - AY ====&lt;br /&gt;
You can access the AY soundchip be outputting to the following ports:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld bc,0xfffd&lt;br /&gt;
ld a, ay register number&lt;br /&gt;
out (c),a&lt;br /&gt;
ld b,0xbf	&lt;br /&gt;
ld a, data byte&lt;br /&gt;
out (c),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about the soundchip, check out:&lt;br /&gt;
https://www.atarimagazines.com/v4n7/stsound.html&lt;br /&gt;
&lt;br /&gt;
=== Small demos with documented source code ===&lt;br /&gt;
* Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691&lt;br /&gt;
* Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123&lt;br /&gt;
* Muse by Ate Bit https://www.pouet.net/prod.php?which=62880&lt;br /&gt;
* Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553&lt;br /&gt;
* Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696&lt;br /&gt;
* Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074&lt;br /&gt;
* Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
I found resources on ZX Spectrum sizecoding to be sparse.&lt;br /&gt;
* All kinds of Z80 Information http://www.z80.info/index.htm&lt;br /&gt;
* Another source for good Z80 Information http://z80-heaven.wikidot.com&lt;br /&gt;
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler&lt;br /&gt;
* Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6&lt;br /&gt;
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&amp;amp;order=type&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&amp;amp;order=type&lt;br /&gt;
* Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/&lt;br /&gt;
* Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/&lt;br /&gt;
&lt;br /&gt;
== Amstrad CPC ==&lt;br /&gt;
The Amstrad consists of a Z80A @ 3.5 MHz CPU &lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up..&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video Display ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
No information yet&lt;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=755</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=755"/>
				<updated>2020-12-22T06:47:26Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: /* Graphics 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 8bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumilator, IX and IY registers) and only a handful of instructions to work with.&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;
== Atari 8bit family ==&lt;br /&gt;
The Atari XE/XL 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 TIA chip, 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;
&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 8bit systems use the Pokey 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;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are sparse&lt;br /&gt;
* Fready's github (link to be added)&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;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;
=== Autoboot ===&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;
Will give you autoboot and more space directly. (though writing through to $0400 will load it onto the screen unless you move the pointers)&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;
* links to be added&lt;br /&gt;
&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;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=6502&amp;diff=754</id>
		<title>6502</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=6502&amp;diff=754"/>
				<updated>2020-12-22T06:46:24Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: /* Graphics 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 8bit micro ARM chip. &lt;br /&gt;
It has only has 3 registers (Accumilator, IX and IY registers) and only a handful of instructions to work with.&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;
== Atari 8bit family ==&lt;br /&gt;
The Atari XE/XL 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 TIA chip, 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;
&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 8bit systems use the Pokey 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;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
Sizecoding resource for the Atari 8bit are sparse&lt;br /&gt;
* Fready's github (link to be added)&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;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;
=== Autoboot ===&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;
Will give you autoboot and more space directly. (though writing through to $0400 will load it onto the screen unless you move the pointers)&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;
* links to be added&lt;br /&gt;
&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 modes280x192,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;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Z80&amp;diff=753</id>
		<title>Z80</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Z80&amp;diff=753"/>
				<updated>2020-12-22T06:38:50Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a Z80 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== Registers ===&lt;br /&gt;
The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities.&lt;br /&gt;
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.&lt;br /&gt;
These are the register pairs of the Z80, as seen from a X86 programmers perspective.&lt;br /&gt;
&lt;br /&gt;
* AF = AL + Flags&lt;br /&gt;
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.&lt;br /&gt;
* BC = Can be seen as CX (B=CH,C=CL), often used for loops &lt;br /&gt;
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting&lt;br /&gt;
* IX = 16 bit Index Register X, can also be accessed with IXH,IXL &lt;br /&gt;
* IY = 16 bit Index Register Y, can also be accessed with IYH,IYL&lt;br /&gt;
&lt;br /&gt;
For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:&lt;br /&gt;
* EX AF,AF' = Swaps AF with AF'&lt;br /&gt;
* EXX = Swaps BC, DE and HL with BC', DE' and HL' &lt;br /&gt;
There are no shadow registers for the index registers.&lt;br /&gt;
&lt;br /&gt;
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers. &lt;br /&gt;
The Sjasmplus assembler doesn't really do proper syntax checking for this so beware.&lt;br /&gt;
&lt;br /&gt;
=== Instructions ===&lt;br /&gt;
Here is a rough translation for some of the Z80 instructions:&lt;br /&gt;
&lt;br /&gt;
* BIT = TEST&lt;br /&gt;
* CP = CMP  (although the Z80 has many other handy compare functionality)&lt;br /&gt;
* DJNZ = LOOP (decreases B and checks not zero)&lt;br /&gt;
* EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA&lt;br /&gt;
* EX = XCHG&lt;br /&gt;
* HALT = HLT&lt;br /&gt;
* JP = JMP&lt;br /&gt;
* JR = JMP NEAR (Jump Relative)&lt;br /&gt;
* LD = MOV&lt;br /&gt;
* LDI = MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++)&lt;br /&gt;
* LDIR = REP MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Learning Z80 Assembler ===&lt;br /&gt;
There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-)  &lt;br /&gt;
&lt;br /&gt;
* http://www.z80.info/lesson1.htm&lt;br /&gt;
* http://www.z80.info/lesson2.htm&lt;br /&gt;
* http://www.z80.info/lesson3.htm&lt;br /&gt;
* http://www.z80.info/lesson4.htm&lt;br /&gt;
* http://www.z80.info/lesson5.htm&lt;br /&gt;
&lt;br /&gt;
There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.&lt;br /&gt;
&lt;br /&gt;
Also, here is a compact 'cheat sheet' with some basics for various Z80 systems:&lt;br /&gt;
https://www.chibiakumas.com/z80/CheatSheet.pdf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ZX Spectrum  ==&lt;br /&gt;
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: SJASMPLUS -This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://sourceforge.net/projects/sjasmplus/&lt;br /&gt;
* Pasmo - for the .TAP create. Available at : http://pasmo.speccy.org&lt;br /&gt;
* Emulator(s): I Found [https://www.zophar.net/sinclair/zx-spin.html ZX Sping], [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.&lt;br /&gt;
&lt;br /&gt;
=== Start values ===&lt;br /&gt;
Upon startup (when called from basic), the following values can assumed:&lt;br /&gt;
&lt;br /&gt;
* The alternate HL register is set to 0x2758&lt;br /&gt;
* BC = start address&lt;br /&gt;
* A = C&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory. &lt;br /&gt;
It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.&lt;br /&gt;
&lt;br /&gt;
Address = 010RRLLL RRRCCCCC &lt;br /&gt;
&lt;br /&gt;
* where RRRRR is the row number (0..23)&lt;br /&gt;
* CCCCC is the column number (0..31)&lt;br /&gt;
* LLL is the line number within the cell (0..7)&lt;br /&gt;
&lt;br /&gt;
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout&lt;br /&gt;
&lt;br /&gt;
==== Adding Color ====&lt;br /&gt;
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile. &lt;br /&gt;
It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.&lt;br /&gt;
&lt;br /&gt;
color = brightness(64) | (PAPER&amp;lt;&amp;lt;3) | INK&lt;br /&gt;
&lt;br /&gt;
Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.&lt;br /&gt;
&lt;br /&gt;
==== Border Color ====&lt;br /&gt;
You can set the border color to any of the 8 colors with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld a,0 ; bottom three bits of a contain the color&lt;br /&gt;
out (254),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Configuration ===&lt;br /&gt;
128K separated to 8 pages (16384 bytes size)&lt;br /&gt;
Default configuration is:&lt;br /&gt;
&lt;br /&gt;
: page 5: $4000-$7fff&lt;br /&gt;
: page 2: $8000-$Bfff&lt;br /&gt;
: page 0: $C000-$Ffff&lt;br /&gt;
&lt;br /&gt;
There are two screens - page 7 and page 5.&lt;br /&gt;
port $7FFD allow to control memory and screens:&lt;br /&gt;
: bits 0-2 - page number mapped to memory at $C0000&lt;br /&gt;
: bit 3 - Select page 5(0) or page 7(1) to be displayed.&lt;br /&gt;
: bit 4 - ROM Select. 0-128K,1-48K&lt;br /&gt;
&lt;br /&gt;
Example of use&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
loop: ei&lt;br /&gt;
 halt&lt;br /&gt;
pg: ld a,$17&lt;br /&gt;
 ld bc,$7ffd&lt;br /&gt;
 out (c),a&lt;br /&gt;
 xor $0A&lt;br /&gt;
 ld (pg+1),a&lt;br /&gt;
... do something with $C000-$DB00&lt;br /&gt;
 jp loop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$17 is use 48K, map page 7&lt;br /&gt;
After xor $0A we'll get value $1D(use 48K ROM, display screen at page 7 and map page5 at $C000).This is so-called &amp;quot;double buffering&amp;quot;.&lt;br /&gt;
See also : https://worldofspectrum.org/faq/reference/128kreference.htm&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
 ld de,5800h&lt;br /&gt;
 ld b,24&lt;br /&gt;
yloop:&lt;br /&gt;
   ld c,32&lt;br /&gt;
xloop:&lt;br /&gt;
   ld a,c&lt;br /&gt;
   and b&lt;br /&gt;
   and 7    ; make sure range is 0..7&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
   jr nz,xloop&lt;br /&gt;
djnz yloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using a Backbuffer ====&lt;br /&gt;
While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
halt               ; synchronize &lt;br /&gt;
ld hl,BACKBUFFER   &lt;br /&gt;
ld de,5800h&lt;br /&gt;
ld bc,768&lt;br /&gt;
ldir&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
	; main loop starts here&lt;br /&gt;
	ld	a,00010111b	; set up memory banks and screen here&lt;br /&gt;
mainloop&lt;br /&gt;
	halt			; sync&lt;br /&gt;
	xor	00001010b	; flip screens&lt;br /&gt;
	out	($fd),a&lt;br /&gt;
	push	af&lt;br /&gt;
&lt;br /&gt;
	; render code goes here&lt;br /&gt;
	; screen buffer is location at 0xC000 instead of 0x4000&lt;br /&gt;
&lt;br /&gt;
	pop	af&lt;br /&gt;
	jr	mainloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Overlaying simple graphics ====&lt;br /&gt;
If you don't want to use a solid color/tile for. &lt;br /&gt;
You could copy a single tile across the screen at startup for some flair.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
   ld a,0x55   ; 01010101 pattern&lt;br /&gt;
   ld bc,0x1800&lt;br /&gt;
copyloop:&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
jr nz,copyloop&lt;br /&gt;
djnz copyloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively you could generate a pattern using logic operations or random noise/data.&lt;br /&gt;
==== Useful routines ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;calculate address of next line,HL=address&lt;br /&gt;
down_hl:&lt;br /&gt;
 INC h&lt;br /&gt;
 LD A,h&lt;br /&gt;
 AND 7&lt;br /&gt;
 RET NZ&lt;br /&gt;
 LD A,L&lt;br /&gt;
 ADD A,#20&lt;br /&gt;
 LD L,A&lt;br /&gt;
 RET C&lt;br /&gt;
 LD A,H&lt;br /&gt;
 SUB 8&lt;br /&gt;
 LD H,A&lt;br /&gt;
 RET&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;move up at screen, HL=address&lt;br /&gt;
up_hl&lt;br /&gt;
     LD A,H&lt;br /&gt;
     DEC H&lt;br /&gt;
     AND 7&lt;br /&gt;
     ret nz&lt;br /&gt;
     LD A,L&lt;br /&gt;
     SUB 32&lt;br /&gt;
     LD L,A&lt;br /&gt;
     ret c&lt;br /&gt;
     LD A,H&lt;br /&gt;
     ADD A,8&lt;br /&gt;
     LD H,A&lt;br /&gt;
 ret&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;write pixel&lt;br /&gt;
 CALL  8933 ; C=X(0..255),B=Y(0..175),5C7D=COORDS&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;Calculate screen Address, IN : A=Y coordinate, OUT : HL=address&lt;br /&gt;
py2saddr:&lt;br /&gt;
        ld l,a&lt;br /&gt;
;	ld a,l&lt;br /&gt;
	and $07&lt;br /&gt;
	ld h,a&lt;br /&gt;
	ld a,l&lt;br /&gt;
	and $c0&lt;br /&gt;
	rra&lt;br /&gt;
	inc a&lt;br /&gt;
	rrca&lt;br /&gt;
	rrca&lt;br /&gt;
	or h&lt;br /&gt;
	ld h,a&lt;br /&gt;
	ld a,l&lt;br /&gt;
	add a&lt;br /&gt;
	add a&lt;br /&gt;
	and $e0&lt;br /&gt;
	ld l,a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;Calculate Attribute address, IB: reg D=Y,reg E=X, OUT: HL=address, destroys DE&lt;br /&gt;
 ld l,d&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 ld h,$11&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 add hl,hl&lt;br /&gt;
 ld d,0&lt;br /&gt;
 add hl,de&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker.&lt;br /&gt;
Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - Beeper ====&lt;br /&gt;
Setting/toggling bit 4 of port 254 will enable the beeper speaker.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
		ld a,$10	;Bit 4 set&lt;br /&gt;
beeploop:	xor $10		;toggle Bit 4&lt;br /&gt;
		out (254),a&lt;br /&gt;
		ld b,90		;wait&lt;br /&gt;
		djnz $&lt;br /&gt;
		jp beeploop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that the bottom three bits of port 254 also set the border color (see above).&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - AY ====&lt;br /&gt;
You can access the AY soundchip be outputting to the following ports:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld bc,0xfffd&lt;br /&gt;
ld a, ay register number&lt;br /&gt;
out (c),a&lt;br /&gt;
ld b,0xbf	&lt;br /&gt;
ld a, data byte&lt;br /&gt;
out (c),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about the soundchip, check out:&lt;br /&gt;
https://www.atarimagazines.com/v4n7/stsound.html&lt;br /&gt;
&lt;br /&gt;
=== Small demos with documented source code ===&lt;br /&gt;
* Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691&lt;br /&gt;
* Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123&lt;br /&gt;
* Muse by Ate Bit https://www.pouet.net/prod.php?which=62880&lt;br /&gt;
* Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553&lt;br /&gt;
* Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696&lt;br /&gt;
* Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074&lt;br /&gt;
* Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
I found resources on ZX Spectrum sizecoding to be sparse.&lt;br /&gt;
* All kinds of Z80 Information http://www.z80.info/index.htm&lt;br /&gt;
* Another source for good Z80 Information http://z80-heaven.wikidot.com&lt;br /&gt;
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler&lt;br /&gt;
* Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6&lt;br /&gt;
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&amp;amp;order=type&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&amp;amp;order=type&lt;br /&gt;
* Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/&lt;br /&gt;
* Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/&lt;br /&gt;
&lt;br /&gt;
== Amstrad CPC ==&lt;br /&gt;
The Amstrad consists of a Z80A @ 3.5 MHz CPU &lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up..&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video Display ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
No information yet&lt;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Z80&amp;diff=752</id>
		<title>Z80</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Z80&amp;diff=752"/>
				<updated>2020-12-22T06:25:07Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a Z80 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== Registers ===&lt;br /&gt;
The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities.&lt;br /&gt;
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.&lt;br /&gt;
These are the register pairs of the Z80, as seen from a X86 programmers perspective.&lt;br /&gt;
&lt;br /&gt;
* AF = AL + Flags&lt;br /&gt;
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.&lt;br /&gt;
* BC = Can be seen as CX (B=CH,C=CL), often used for loops &lt;br /&gt;
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting&lt;br /&gt;
* IX = 16 bit Index Register X, can also be accessed with IXH,IXL &lt;br /&gt;
* IY = 16 bit Index Register Y, can also be accessed with IYH,IYL&lt;br /&gt;
&lt;br /&gt;
For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:&lt;br /&gt;
* EX AF,AF' = Swaps AF with AF'&lt;br /&gt;
* EXX = Swaps BC, DE and HL with BC', DE' and HL' &lt;br /&gt;
There are no shadow registers for the index registers.&lt;br /&gt;
&lt;br /&gt;
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers. &lt;br /&gt;
The Sjasmplus assembler doesn't really do proper syntax checking for this so beware.&lt;br /&gt;
&lt;br /&gt;
=== Instructions ===&lt;br /&gt;
Here is a rough translation for some of the Z80 instructions:&lt;br /&gt;
&lt;br /&gt;
* BIT = TEST&lt;br /&gt;
* CP = CMP  (although the Z80 has many other handy compare functionality)&lt;br /&gt;
* DJNZ = LOOP (decreases B and checks not zero)&lt;br /&gt;
* EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA&lt;br /&gt;
* EX = XCHG&lt;br /&gt;
* HALT = HLT&lt;br /&gt;
* JP = JMP&lt;br /&gt;
* JR = JMP NEAR (Jump Relative)&lt;br /&gt;
* LD = MOV&lt;br /&gt;
* LDI = MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++)&lt;br /&gt;
* LDIR = REP MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Learning Z80 Assembler ===&lt;br /&gt;
There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-)  &lt;br /&gt;
&lt;br /&gt;
* http://www.z80.info/lesson1.htm&lt;br /&gt;
* http://www.z80.info/lesson2.htm&lt;br /&gt;
* http://www.z80.info/lesson3.htm&lt;br /&gt;
* http://www.z80.info/lesson4.htm&lt;br /&gt;
* http://www.z80.info/lesson5.htm&lt;br /&gt;
&lt;br /&gt;
There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.&lt;br /&gt;
&lt;br /&gt;
Also, here is a compact 'cheat sheet' with some basics for various Z80 systems:&lt;br /&gt;
https://www.chibiakumas.com/z80/CheatSheet.pdf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ZX Spectrum  ==&lt;br /&gt;
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: SJASMPLUS -This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://sourceforge.net/projects/sjasmplus/&lt;br /&gt;
* Pasmo - for the .TAP create. Available at : http://pasmo.speccy.org&lt;br /&gt;
* Emulator(s): I Found [https://www.zophar.net/sinclair/zx-spin.html ZX Sping], [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.&lt;br /&gt;
&lt;br /&gt;
=== Start values ===&lt;br /&gt;
Upon startup (when called from basic), the following values can assumed:&lt;br /&gt;
&lt;br /&gt;
* The alternate HL register is set to 0x2758&lt;br /&gt;
* BC = start address&lt;br /&gt;
* A = C&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory. &lt;br /&gt;
It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.&lt;br /&gt;
&lt;br /&gt;
Address = 010RRLLL RRRCCCCC &lt;br /&gt;
&lt;br /&gt;
* where RRRRR is the row number (0..23)&lt;br /&gt;
* CCCCC is the column number (0..31)&lt;br /&gt;
* LLL is the line number within the cell (0..7)&lt;br /&gt;
&lt;br /&gt;
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout&lt;br /&gt;
&lt;br /&gt;
==== Adding Color ====&lt;br /&gt;
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile. &lt;br /&gt;
It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.&lt;br /&gt;
&lt;br /&gt;
color = brightness(64) | (PAPER&amp;lt;&amp;lt;3) | INK&lt;br /&gt;
&lt;br /&gt;
Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.&lt;br /&gt;
&lt;br /&gt;
==== Border Color ====&lt;br /&gt;
You can set the border color to any of the 8 colors with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld a,0 ; bottom three bits of a contain the color&lt;br /&gt;
out (254),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Configuration ===&lt;br /&gt;
128K separated to 8 pages (16384 bytes size)&lt;br /&gt;
Default configuration is:&lt;br /&gt;
&lt;br /&gt;
: page 5: $4000-$7fff&lt;br /&gt;
: page 2: $8000-$Bfff&lt;br /&gt;
: page 0: $C000-$Ffff&lt;br /&gt;
&lt;br /&gt;
There are two screens - page 7 and page 5.&lt;br /&gt;
port $7FFD allow to control memory and screens:&lt;br /&gt;
: bits 0-2 - page number mapped to memory at $C0000&lt;br /&gt;
: bit 3 - Select page 5(0) or page 7(1) to be displayed.&lt;br /&gt;
: bit 4 - ROM Select. 0-128K,1-48K&lt;br /&gt;
&lt;br /&gt;
Example of use&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
loop: ei&lt;br /&gt;
 halt&lt;br /&gt;
pg: ld a,$17&lt;br /&gt;
 ld bc,$7ffd&lt;br /&gt;
 out (c),a&lt;br /&gt;
 xor $0A&lt;br /&gt;
 ld (pg+1),a&lt;br /&gt;
... do something with $C000-$DB00&lt;br /&gt;
 jp loop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$17 is use 48K, map page 7&lt;br /&gt;
After xor $0A we'll get value $1D(use 48K ROM, display screen at page 7 and map page5 at $C000).This is so-called &amp;quot;double buffering&amp;quot;.&lt;br /&gt;
See also : https://worldofspectrum.org/faq/reference/128kreference.htm&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
 ld de,5800h&lt;br /&gt;
 ld b,24&lt;br /&gt;
yloop:&lt;br /&gt;
   ld c,32&lt;br /&gt;
xloop:&lt;br /&gt;
   ld a,c&lt;br /&gt;
   and b&lt;br /&gt;
   and 7    ; make sure range is 0..7&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
   jr nz,xloop&lt;br /&gt;
djnz yloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using a Backbuffer ====&lt;br /&gt;
While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
halt               ; synchronize &lt;br /&gt;
ld hl,BACKBUFFER   &lt;br /&gt;
ld de,5800h&lt;br /&gt;
ld bc,768&lt;br /&gt;
ldir&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
	; main loop starts here&lt;br /&gt;
	ld	a,00010111b	; set up memory banks and screen here&lt;br /&gt;
mainloop&lt;br /&gt;
	halt			; sync&lt;br /&gt;
	xor	00001010b	; flip screens&lt;br /&gt;
	out	($fd),a&lt;br /&gt;
	push	af&lt;br /&gt;
&lt;br /&gt;
	; render code goes here&lt;br /&gt;
	; screen buffer is location at 0xC000 instead of 0x4000&lt;br /&gt;
&lt;br /&gt;
	pop	af&lt;br /&gt;
	jr	mainloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Overlaying simple graphics ====&lt;br /&gt;
If you don't want to use a solid color/tile for. &lt;br /&gt;
You could copy a single tile across the screen at startup for some flair.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
   ld a,0x55   ; 01010101 pattern&lt;br /&gt;
   ld bc,0x1800&lt;br /&gt;
copyloop:&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
jr nz,copyloop&lt;br /&gt;
djnz copyloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively you could generate a pattern using logic operations or random noise/data.&lt;br /&gt;
==== Useful routines ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
;calculate address of next line,HL=address&lt;br /&gt;
down_hl:&lt;br /&gt;
 INC h&lt;br /&gt;
 LD A,h&lt;br /&gt;
 AND 7&lt;br /&gt;
 RET NZ&lt;br /&gt;
 LD A,L&lt;br /&gt;
 ADD A,#20&lt;br /&gt;
 LD L,A&lt;br /&gt;
 RET C&lt;br /&gt;
 LD A,H&lt;br /&gt;
 SUB 8&lt;br /&gt;
 LD H,A&lt;br /&gt;
 RET&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker.&lt;br /&gt;
Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - Beeper ====&lt;br /&gt;
Setting/toggling bit 4 of port 254 will enable the beeper speaker.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
		ld a,$10	;Bit 4 set&lt;br /&gt;
beeploop:	xor $10		;toggle Bit 4&lt;br /&gt;
		out (254),a&lt;br /&gt;
		ld b,90		;wait&lt;br /&gt;
		djnz $&lt;br /&gt;
		jp beeploop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that the bottom three bits of port 254 also set the border color (see above).&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - AY ====&lt;br /&gt;
You can access the AY soundchip be outputting to the following ports:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld bc,0xfffd&lt;br /&gt;
ld a, ay register number&lt;br /&gt;
out (c),a&lt;br /&gt;
ld b,0xbf	&lt;br /&gt;
ld a, data byte&lt;br /&gt;
out (c),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about the soundchip, check out:&lt;br /&gt;
https://www.atarimagazines.com/v4n7/stsound.html&lt;br /&gt;
&lt;br /&gt;
=== Small demos with documented source code ===&lt;br /&gt;
* Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691&lt;br /&gt;
* Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123&lt;br /&gt;
* Muse by Ate Bit https://www.pouet.net/prod.php?which=62880&lt;br /&gt;
* Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553&lt;br /&gt;
* Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696&lt;br /&gt;
* Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074&lt;br /&gt;
* Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
I found resources on ZX Spectrum sizecoding to be sparse.&lt;br /&gt;
* All kinds of Z80 Information http://www.z80.info/index.htm&lt;br /&gt;
* Another source for good Z80 Information http://z80-heaven.wikidot.com&lt;br /&gt;
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler&lt;br /&gt;
* Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6&lt;br /&gt;
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&amp;amp;order=type&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&amp;amp;order=type&lt;br /&gt;
* Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/&lt;br /&gt;
* Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/&lt;br /&gt;
&lt;br /&gt;
== Amstrad CPC ==&lt;br /&gt;
The Amstrad consists of a Z80A @ 3.5 MHz CPU &lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up..&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video Display ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
No information yet&lt;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	<entry>
		<id>http://www.sizecoding.org/index.php?title=Z80&amp;diff=751</id>
		<title>Z80</title>
		<link rel="alternate" type="text/html" href="http://www.sizecoding.org/index.php?title=Z80&amp;diff=751"/>
				<updated>2020-12-19T14:34:14Z</updated>
		
		<summary type="html">&lt;p&gt;G0blinish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Wanting to start sizecoding on a Z80 platform in this day and age can be tough. &lt;br /&gt;
&lt;br /&gt;
So here is a bit of help to get you started:&lt;br /&gt;
&lt;br /&gt;
=== Registers ===&lt;br /&gt;
The Z80 can be seen as the little 8-bit brother of X86 chipsets, with many similarities.&lt;br /&gt;
If you are coming from a X86 background, this might help you get a bit more grip on the Z80.&lt;br /&gt;
These are the register pairs of the Z80, as seen from a X86 programmers perspective.&lt;br /&gt;
&lt;br /&gt;
* AF = AL + Flags&lt;br /&gt;
* HL = Can be seen as BX (H=BH,L=BL) or SI in a (HL) setting, like BX also used for addressing.&lt;br /&gt;
* BC = Can be seen as CX (B=CH,C=CL), often used for loops &lt;br /&gt;
* DE = Can be seen as DX (D=DH,E=DL) or DI in a (DE) setting&lt;br /&gt;
* IX = 16 bit Index Register X, can also be accessed with IXH,IXL &lt;br /&gt;
* IY = 16 bit Index Register Y, can also be accessed with IYH,IYL&lt;br /&gt;
&lt;br /&gt;
For each of the main registers there also exists a shadow register. These cannot be accessed directly, but must be swapped in and out with the main register set. The shadow registers are usually denoted by the ' symbol. They can be swapped with the following commands:&lt;br /&gt;
* EX AF,AF' = Swaps AF with AF'&lt;br /&gt;
* EXX = Swaps BC, DE and HL with BC', DE' and HL' &lt;br /&gt;
There are no shadow registers for the index registers.&lt;br /&gt;
&lt;br /&gt;
Note: For a lot of operations, you can only use the A(8bit) and HL(16bit) registers. &lt;br /&gt;
The Sjasmplus assembler doesn't really do proper syntax checking for this so beware.&lt;br /&gt;
&lt;br /&gt;
=== Instructions ===&lt;br /&gt;
Here is a rough translation for some of the Z80 instructions:&lt;br /&gt;
&lt;br /&gt;
* BIT = TEST&lt;br /&gt;
* CP = CMP  (although the Z80 has many other handy compare functionality)&lt;br /&gt;
* DJNZ = LOOP (decreases B and checks not zero)&lt;br /&gt;
* EXE = Exchange all registers with Shadow registers, can be used a bit like PUSHA/POPA&lt;br /&gt;
* EX = XCHG&lt;br /&gt;
* HALT = HLT&lt;br /&gt;
* JP = JMP&lt;br /&gt;
* JR = JMP NEAR (Jump Relative)&lt;br /&gt;
* LD = MOV&lt;br /&gt;
* LDI = MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++)&lt;br /&gt;
* LDIR = REP MOVSB   (tmp=(HL),(DE)=tmp, DE++, HL++, BC--)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Learning Z80 Assembler ===&lt;br /&gt;
There are many Z80 tutorials available online, but one i found very simple and clear is at this 1996 styled webpage ;-)  &lt;br /&gt;
&lt;br /&gt;
* http://www.z80.info/lesson1.htm&lt;br /&gt;
* http://www.z80.info/lesson2.htm&lt;br /&gt;
* http://www.z80.info/lesson3.htm&lt;br /&gt;
* http://www.z80.info/lesson4.htm&lt;br /&gt;
* http://www.z80.info/lesson5.htm&lt;br /&gt;
&lt;br /&gt;
There is no proper index-page for this, which is why i linked all the lessons above, but you can continue to the next lesson by clicking at the next lesson at the bottom of the page.&lt;br /&gt;
&lt;br /&gt;
Also, here is a compact 'cheat sheet' with some basics for various Z80 systems:&lt;br /&gt;
https://www.chibiakumas.com/z80/CheatSheet.pdf&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ZX Spectrum  ==&lt;br /&gt;
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.&lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:&lt;br /&gt;
&lt;br /&gt;
* Assembler: SJASMPLUS -This assembler has nice macros for creating Binaries and SNA snapshot files out of the box. You can download it at https://sourceforge.net/projects/sjasmplus/&lt;br /&gt;
* Pasmo - for the .TAP create. Available at : http://pasmo.speccy.org&lt;br /&gt;
* Emulator(s): I Found [https://www.zophar.net/sinclair/zx-spin.html ZX Sping], [https://sourceforge.net/projects/fuse-emulator/ FUSE], [https://sourceforge.net/projects/unrealspeccyp/ UnrealSpeccy] and [https://www.aptanet.org/eightyone/ EightyOne] to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.&lt;br /&gt;
&lt;br /&gt;
=== Start values ===&lt;br /&gt;
Upon startup (when called from basic), the following values can assumed:&lt;br /&gt;
&lt;br /&gt;
* The alternate HL register is set to 0x2758&lt;br /&gt;
* BC = start address&lt;br /&gt;
* A = C&lt;br /&gt;
&lt;br /&gt;
=== Video display ===&lt;br /&gt;
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory. &lt;br /&gt;
It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.&lt;br /&gt;
&lt;br /&gt;
Address = 010RRLLL RRRCCCCC &lt;br /&gt;
&lt;br /&gt;
* where RRRRR is the row number (0..23)&lt;br /&gt;
* CCCCC is the column number (0..31)&lt;br /&gt;
* LLL is the line number within the cell (0..7)&lt;br /&gt;
&lt;br /&gt;
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout&lt;br /&gt;
&lt;br /&gt;
==== Adding Color ====&lt;br /&gt;
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile. &lt;br /&gt;
It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.&lt;br /&gt;
&lt;br /&gt;
color = brightness(64) | (PAPER&amp;lt;&amp;lt;3) | INK&lt;br /&gt;
&lt;br /&gt;
Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.&lt;br /&gt;
&lt;br /&gt;
==== Border Color ====&lt;br /&gt;
You can set the border color to any of the 8 colors with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld a,0 ; bottom three bits of a contain the color&lt;br /&gt;
out (254),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Memory Configuration ===&lt;br /&gt;
128K separated to 8 pages (16384 bytes size)&lt;br /&gt;
Default configuration is:&lt;br /&gt;
page 5: $4000-$7fff&lt;br /&gt;
page 2: $8000-$Bfff&lt;br /&gt;
page 0: $C000-$Ffff&lt;br /&gt;
There are two screens - page 7 and page 5.&lt;br /&gt;
port $7FFD allow to control memory and screens:&lt;br /&gt;
bits 0-2 - page number mapped to memory at $C0000&lt;br /&gt;
bit 3 - Select page 5(0) or page 7(1) to be displayed.&lt;br /&gt;
bit 4 - ROM Select. 0-128K,1-48K&lt;br /&gt;
&lt;br /&gt;
Example of use&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
loop: ei&lt;br /&gt;
 halt&lt;br /&gt;
pg: ld a,$17&lt;br /&gt;
 ld bc,$7ffd&lt;br /&gt;
 out (c),a&lt;br /&gt;
 xor $0A&lt;br /&gt;
 ld (pg+1),a&lt;br /&gt;
... do something with $C000-$DB00&lt;br /&gt;
 jp loop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$17 is use 48K, map page 7&lt;br /&gt;
After xor $0A we'll get value $1D(use 48K ROM, display screen at page 7 and map page5 at $C000).This is so-called &amp;quot;double buffering&amp;quot;.&lt;br /&gt;
See also : https://worldofspectrum.org/faq/reference/128kreference.htm&lt;br /&gt;
&lt;br /&gt;
==== Getting something on screen ====&lt;br /&gt;
Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
 ld de,5800h&lt;br /&gt;
 ld b,24&lt;br /&gt;
yloop:&lt;br /&gt;
   ld c,32&lt;br /&gt;
xloop:&lt;br /&gt;
   ld a,c&lt;br /&gt;
   and b&lt;br /&gt;
   and 7    ; make sure range is 0..7&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
   jr nz,xloop&lt;br /&gt;
djnz yloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using a Backbuffer ====&lt;br /&gt;
While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
halt               ; synchronize &lt;br /&gt;
ld hl,BACKBUFFER   &lt;br /&gt;
ld de,5800h&lt;br /&gt;
ld bc,768&lt;br /&gt;
ldir&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
	; main loop starts here&lt;br /&gt;
	ld	a,00010111b	; set up memory banks and screen here&lt;br /&gt;
mainloop&lt;br /&gt;
	halt			; sync&lt;br /&gt;
	xor	00001010b	; flip screens&lt;br /&gt;
	out	($fd),a&lt;br /&gt;
	push	af&lt;br /&gt;
&lt;br /&gt;
	; render code goes here&lt;br /&gt;
	; screen buffer is location at 0xC000 instead of 0x4000&lt;br /&gt;
&lt;br /&gt;
	pop	af&lt;br /&gt;
	jr	mainloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Overlaying simple graphics ====&lt;br /&gt;
If you don't want to use a solid color/tile for. &lt;br /&gt;
You could copy a single tile across the screen at startup for some flair.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
   ld a,0x55   ; 01010101 pattern&lt;br /&gt;
   ld bc,0x1800&lt;br /&gt;
copyloop:&lt;br /&gt;
   ld (de),a&lt;br /&gt;
   inc de&lt;br /&gt;
   dec c&lt;br /&gt;
jr nz,copyloop&lt;br /&gt;
djnz copyloop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively you could generate a pattern using logic operations or random noise/data.&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker.&lt;br /&gt;
Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - Beeper ====&lt;br /&gt;
Setting/toggling bit 4 of port 254 will enable the beeper speaker.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
		ld a,$10	;Bit 4 set&lt;br /&gt;
beeploop:	xor $10		;toggle Bit 4&lt;br /&gt;
		out (254),a&lt;br /&gt;
		ld b,90		;wait&lt;br /&gt;
		djnz $&lt;br /&gt;
		jp beeploop&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that the bottom three bits of port 254 also set the border color (see above).&lt;br /&gt;
&lt;br /&gt;
==== Make some noise - AY ====&lt;br /&gt;
You can access the AY soundchip be outputting to the following ports:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;z80&amp;quot;&amp;gt;&lt;br /&gt;
ld bc,0xfffd&lt;br /&gt;
ld a, ay register number&lt;br /&gt;
out (c),a&lt;br /&gt;
ld b,0xbf	&lt;br /&gt;
ld a, data byte&lt;br /&gt;
out (c),a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about the soundchip, check out:&lt;br /&gt;
https://www.atarimagazines.com/v4n7/stsound.html&lt;br /&gt;
&lt;br /&gt;
=== Small demos with documented source code ===&lt;br /&gt;
* Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691&lt;br /&gt;
* Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123&lt;br /&gt;
* Muse by Ate Bit https://www.pouet.net/prod.php?which=62880&lt;br /&gt;
* Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553&lt;br /&gt;
* Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696&lt;br /&gt;
* Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074&lt;br /&gt;
* Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
I found resources on ZX Spectrum sizecoding to be sparse.&lt;br /&gt;
* All kinds of Z80 Information http://www.z80.info/index.htm&lt;br /&gt;
* Another source for good Z80 Information http://z80-heaven.wikidot.com&lt;br /&gt;
* Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler&lt;br /&gt;
* Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6&lt;br /&gt;
* Blogpost on ZX Spectrum coding from a X86 coder's perspectove on superogue's sizecdoing blog (soon)&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by goblinish https://www.pouet.net/groups.php?which=11696&amp;amp;order=type&lt;br /&gt;
* 128byte/256 byte zx spectrum productions by gasman https://www.pouet.net/groups.php?which=1218&amp;amp;order=type&lt;br /&gt;
* Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/&lt;br /&gt;
* Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/&lt;br /&gt;
&lt;br /&gt;
== Amstrad CPC ==&lt;br /&gt;
The Amstrad consists of a Z80A @ 3.5 MHz CPU &lt;br /&gt;
&lt;br /&gt;
=== Setting up ===&lt;br /&gt;
Setting up..&lt;br /&gt;
&lt;br /&gt;
* Assembler: -&lt;br /&gt;
* Emulator(s): -&lt;br /&gt;
&lt;br /&gt;
=== Video Display ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Sound ===&lt;br /&gt;
No information yet&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
No information yet&lt;/div&gt;</summary>
		<author><name>G0blinish</name></author>	</entry>

	</feed>