aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-11-25 18:20:15 +0000
committerDimitri Sokolyuk <demon@dim13.org>2015-11-25 18:20:15 +0000
commit76369877147e77c24e6b56f4a166922959c2f149 (patch)
tree4baf650be4971f0577c4a363629099aeddb5a462
parent4592509f18e44d6302fe3fd7ad69974beb00b749 (diff)
Add examples
-rw-r--r--examples/0x42c.dasm16317
-rw-r--r--examples/Matrix digital rain.dasm16114
-rw-r--r--examples/Tetris.d125
-rw-r--r--examples/Tetris.dasm16424
-rw-r--r--examples/alpha.s19
-rw-r--r--examples/brk.s1
-rw-r--r--examples/colour_cycle.s15
-rw-r--r--examples/dcpu.corebin0 -> 902576 bytes
-rw-r--r--examples/disp.s14
-rw-r--r--examples/hello.asm18
-rw-r--r--examples/hello2.asm37
-rw-r--r--examples/ique_fibonacci.asm12
-rw-r--r--examples/keyb.s12
-rw-r--r--examples/keyboard_reader.asm20
-rw-r--r--examples/matrix.s114
-rw-r--r--examples/matrix2.s276
-rw-r--r--examples/matrix3.s325
-rw-r--r--examples/minesweeper.s1272
-rw-r--r--examples/minesweeper.s.orig1272
-rw-r--r--examples/pix.s74
-rw-r--r--examples/spec_example.asm30
-rw-r--r--examples/t.d125
-rw-r--r--examples/t.s2
-rw-r--r--examples/t2.d72
-rw-r--r--examples/test.dump4
-rw-r--r--examples/test.s28
-rw-r--r--examples/test2.s19
-rw-r--r--examples/test3.s37
-rw-r--r--examples/test4.s13
-rw-r--r--examples/testx.s3
-rw-r--r--examples/x.s15
31 files changed, 4809 insertions, 0 deletions
diff --git a/examples/0x42c.dasm16 b/examples/0x42c.dasm16
new file mode 100644
index 0000000..6938430
--- /dev/null
+++ b/examples/0x42c.dasm16
@@ -0,0 +1,317 @@
+SET PC, initialstate
+
+; string definitions (currently using C-style strings)
+:version dat "0x42c v.0.1 128K MEM READY", 0
+:input dat "> ", 0
+
+; commands
+:reddit dat "reddit", 0
+:date dat "date", 0
+:clean dat "clean", 0
+:vers dat "version", 0
+:echo dat "echo", 0
+
+; outputs
+:redditoutput dat "Reddit, the frontpage of the internet.", 0
+:dateoutput dat "00:00:00 UTC on 1 January 1970", 0
+:unrecognized dat "Unrecognized command '", 0
+:unrecognizedEnd dat "'.", 0
+
+
+:initialstate
+ set [0x1335], 0x8000 ; vidmem
+ set [0x1337], 0x1338 ; input buffer
+
+
+:main
+ set a, version
+ jsr printnl ; printnl(version)
+
+:inputready
+ set a, input
+ jsr print ; printnl(input)
+
+ set pc, kbloop
+
+:kbloop
+ ife [0x9000], 0 ; if there's no input...
+ set pc, kbloop
+ ife [0x9000], 0xA ; enter
+ jsr enteredcommand
+ ife [0x9000], 0x8 ; backspace
+ jsr backspace
+
+ set a, [0x9000]
+ jsr printchar ; printchar([0x9000])
+
+ set a, [0x1337] ; 0x1337 is the pointer to my buffer string
+ set b, [0x9000]
+ jsr strcat ; strcat([0x1337], [0x9000])
+ add [0x1337], 1
+
+ set [0x9000], 0
+
+ set pc, kbloop
+
+:backspace
+ sub [0x1335], 1 ; video pointer
+
+ sub [0x1337], 1 ; delete last char
+
+ set a, [0x1337]
+ set [a], 0
+
+ set a, 0
+ jsr printchar ; printchar(0)
+ sub [0x1335], 1
+ set [0x9000], 0
+ set pc, kbloop
+
+:reset
+ set [0x1337], 0x1338 ; reset pointer
+ set [0x1338], 0 ; reset string
+ set [0x9000], 0 ; reset keyboard
+ set [0x1336], 0 ; reset command recognized
+ set pc, pop
+
+:enteredcommand
+ set a, echo
+ set b, 0x1338
+ jsr startswith ; startswith(echo, 0x1338)
+ ife y, 1
+ set pc, echof
+
+ set a, reddit
+ set b, 0x1338
+ jsr strcmp ; strcmp(reddit, 0x1338)
+ ife y, 1
+ set pc, redditf
+
+ set a, date
+ set b, 0x1338
+ jsr strcmp ; strcmp(date, 0x1338)
+ ife y, 1
+ set pc, datef
+
+ set a, clean
+ set b, 0x1338
+ jsr strcmp ; strcmp(date, 0x1338)
+ ife y, 1
+ set pc, cleanf
+
+ set a, vers
+ set b, 0x1338
+ jsr strcmp ; strcmp(vers, 0x1338)
+ ife y, 1
+ set pc, versionf
+
+ ifn a, 1
+ set pc, unrecognizedf
+
+ :enterdone
+ jsr reset
+
+ set pc, inputready
+
+
+:echof
+ jsr newline
+ set a, echo
+ jsr strlen
+ add a, 1 ; +1 to strlen, for the space
+
+ set b, 0x1338
+ add b, a ; start at `strlen'
+ set a, b ; string pointer
+ jsr printnl
+
+ set a, 1
+ set pc, enterdone
+
+:cleanf
+ jsr clearScreen
+ set [0x1335], 0x8000
+ set a, 1
+ set pc, enterdone
+
+:clearScreen
+ set i, sp
+ set sp, 0x8200
+ :clearScreen_loop
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ set push, 0
+ ifg 0x8000, sp
+ set pc, clearScreen_done
+ set pc, clearScreen_loop
+ :clearScreen_done
+ set sp, i
+ set pc, pop
+
+:versionf
+ jsr newline
+ jsr reset
+ set a, 1 ; return 1
+ set pc, main
+
+:unrecognizedf
+ jsr newline
+ set a, unrecognized
+ jsr print
+ set a, 0x1338
+ jsr print
+ set a, unrecognizedEnd
+ jsr printnl
+
+ set a, 1 ; return 1
+ set pc, enterdone
+
+:redditf
+ set x, 1337
+ jsr printnl
+ set a, redditoutput
+ jsr printnl
+ set a, 1 ; return 1
+ set pc, enterdone
+
+:datef
+ jsr printnl
+ set a, dateoutput
+ jsr printnl
+ set a, 1 ; return 1
+ set pc, enterdone
+
+
+;; STANDARD FUNCTIONS OVER HER
+
+:startswith
+ set y, 0
+
+ ife [a], 0
+ set pc, startswith_end_success
+
+ ife [a], [b]
+ jsr checkzero
+
+ ifn [a], [b]
+ set pc, pop
+
+ ife y, 1
+ set pc, pop
+
+ add a, 1
+ add b, 1
+ set pc, startswith
+
+ :startswith_end_success
+ set y, 1
+ set pc, pop
+
+:strlen
+ set y, 0
+
+ :strlen_begin
+ ife [a], 0
+ set pc, strlen_end
+
+ add y, 1
+ add a, 1
+ set pc, strlen_begin
+
+ :strlen_end
+ set a, y
+ set pc, pop
+
+
+:strcmp
+ set y, 0
+
+ ife [a], [b]
+ jsr checkzero
+
+ ife y, 1
+ set pc, pop
+
+ ifn [a], [b]
+ set pc, pop
+
+ add a, 1
+ add b, 1
+ set pc, strcmp
+
+:checkzero
+ ife [a], 0
+ set y, 1
+ set pc, pop
+
+:strcat
+ set [a], b
+ add a, 1
+ add b, 1
+ set [a], 0
+ set pc, pop
+
+
+:printnl ; shortcut
+ jsr print
+ jsr newline
+ set pc, pop
+
+:print
+ set i, a
+ :doprint
+ ifg [0x1335], 0x81ff ; end of vidmem
+ set [0x1335], 0x8000
+ ife [i], 0
+ set pc, pop
+ set a, [i]
+ jsr printchar
+ add i, 1
+ set pc, doprint
+
+:printchar
+ set b, [0x1335]
+ bor a, 0xf100 ; color :D
+ set [b], a
+ add [0x1335], 1
+ set pc, pop
+
+:newline
+
+ set i, [0x1335]
+ mod i, 32 ; width
+ set z, 32
+ sub z, i
+ add [0x1335], z
+ set pc, pop
+
+:end set pc, end \ No newline at end of file
diff --git a/examples/Matrix digital rain.dasm16 b/examples/Matrix digital rain.dasm16
new file mode 100644
index 0000000..750c7d3
--- /dev/null
+++ b/examples/Matrix digital rain.dasm16
@@ -0,0 +1,114 @@
+set z, 0x1234 ; rand_seed
+set y, z ; cur_rand
+set pc, main_loop
+
+:next_rand
+mul y, 10061
+add y, 1
+set pc, pop
+
+:main_loop
+set i, 0
+set x, y
+set y, z
+set a, 0x8000
+
+:next_row1
+ set j, 0
+ :next_char1
+ jsr next_rand
+ ife [a], 0
+ set pc, skip1
+ ifb y, 0x7000
+ set pc, skip1
+
+ ; mutate char at [a] (j, i)
+ set push, y
+ set y, x
+ jsr next_rand
+ and [a], 0xff00 ; reset char
+ and y, 0x003f
+ bor [a], [code+y] ; set letter
+ set x, y
+ set y, pop
+
+ :skip1
+ add j, 1
+ add a, 1
+ ifg 32, j
+ set pc, next_char1
+ add i, 1
+ ifg 12, i
+ set pc, next_row1
+set y, x
+
+; step 2: move all columns down
+set i, 12
+set a, 0x817f
+
+:next_row2
+ set j, 32
+ :next_char2
+ ife [a], 0
+ set pc, empty_char
+ ifb [a+32], 0xffff
+ set pc, move_down
+
+ ; add new char at [a+32] (j, i+1)
+ jsr next_rand
+ set [a+32], [a]
+ and [a+32], 0xff00
+ set x, y
+ and x, 0x003f
+ bor [a+32], [code+x]
+ and [a], 0x7fff
+ set pc, skip2
+
+ :empty_char
+ set [a+32], 0
+
+ :move_down
+ and [a+32], 0x7fff
+
+ :skip2
+ sub j, 1
+ sub a, 1
+ ifg j, 0
+ set pc, next_char2
+ sub i, 1
+ ifg i, 0
+ set pc, next_row2
+set y, x
+
+; step 3: update top layer
+set a, 0x8000
+set j, 0
+:next_char3
+ jsr next_rand
+ ifb y, 0x0700
+ set pc, skip3
+ ifb [a], 0xffff
+ set pc, empty_char2
+
+ set [a], 0x2000
+ ifb y, 0x0800
+ set [a], 0xa000
+ set x, y
+ and x, 0x003f
+ bor [a], [code+x]
+ set pc, skip3
+
+ :empty_char2
+ set [a], 0
+
+ :skip3
+ add j, 1
+ add a, 1
+ ifg 32, j
+ set pc, next_char3
+
+set PC, main_loop
+
+sub PC, 1
+
+:code dat "00112334567889&||!!@==::**##<>>__TYYUDQZJJIX- ~~oiwlrkm//\\'[]^)`" \ No newline at end of file
diff --git a/examples/Tetris.d b/examples/Tetris.d
new file mode 100644
index 0000000..199a931
--- /dev/null
+++ b/examples/Tetris.d
@@ -0,0 +1,125 @@
+0000: 7c01 8022 7c11 01e6 7c10 005f 7c01 8042
+0008: 7c11 01f7 7c10 005f 8061 8462 7c10 0070
+0010: 800c 7dc1 000d 7de4 006f 8dd9 01e2 006f
+0018: 7de4 006f 8dd9 19e2 006f 7c01 8000 8081
+0020: 8402 01fe 8180 7dc1 001f 7c01 8000 8011
+0028: 7c81 0700 8402 8412 06ae 7dc1 0031 8011
+0030: d802 01fe 8180 7dc1 0028 7c21 f700 7c01
+0038: 8021 7c11 020a 7c10 005f 7c01 8061 7c11
+0040: 0211 7c10 005f 7c01 80a1 7c11 0218 7c10
+0048: 005f 7c01 8000 7d01 000a 0100 7d01 001f
+0050: 0100 7c02 0020 01fe 8180 7dc1 004b 7c10
+0058: 008e 7c10 009d 7c10 007e 7dc1 013e 809c
+0060: 61c1 2481 088a 8402 8412 7dc1 005f 7de4
+0068: 006f 274d 85e2 006f 7801 006f 61c1 c00f
+0070: 7811 007d 7c12 9000 2401 800c 61c1 8091
+0078: 85e2 007d bde9 007d 61c1 0000 7c10 0067
+0080: 9c06 79e1 008a 008c 01e1 008c 7de1 008d
+0088: 8011 61c1 0000 0000 0000 8011 7801 01e5
+0090: 7c21 8048 0011 a816 7c1a e730 04a1 8423
+0098: a805 800e 7dc1 0092 61c1 7801 01e4 7c21
+00a0: 8088 0011 a816 7c1a e730 04a1 8423 a805
+00a8: 800e 7dc1 00a1 61c1 7831 008d 7841 008a
+00b0: 8847 784a 008b 9047 7c42 023c 8061 3011
+00b8: 801c 7dc1 00cb 022e 7dc1 00c6 7c3e 8180
+00c0: 8dc2 80bc 7dc1 00cb 8011 61c1 800c 8011
+00c8: 04b1 0531 0001 8462 8832 8442 8411 c06c
+00d0: 61c1 8c6f 7dc1 00b7 e032 7dc1 00b7 7c31
+00d8: 80e1 7841 008c 9847 7c42 023c 8061 3011
+00e0: 801c 7c11 0700 04b1 0531 0001 8462 8832
+00e8: 8442 8411 c06c 61c1 8c6f 7dc1 00df e032
+00f0: 7dc1 00df 7c01 816b 0011 8071 8061 0431
+00f8: 80bc 7dc1 010a 8832 8462 1aae 7dc1 00f8
+0100: 8472 7c13 0020 907c 7dc1 010a 7c1e 8000
+0108: 7dc1 00f6 040c 7dc1 0117 8061 2481 8462
+0110: 8402 8412 1b4e 7dc1 010e d003 d013 7c03
+0118: 0020 01fe 8000 7dc1 0128 7c13 0020 907c
+0120: 7dc1 010a 05fe 8000 7dc1 010a 7dc1 00f6
+0128: 807c 61c1 1de2 01e3 5c71 01de 7874 01e4
+0130: 1de2 01e5 7c10 008e 7aae 01e3 61c1 81e1
+0138: 01e3 85e2 01e4 7c10 009d 61c1 7c10 007e
+0140: 8801 7c10 00ac 801c 7dc1 01b4 7c10 00d7
+0148: 7801 01e4 41e1 01c9 01ca 7851 01c9 8401
+0150: 7c10 00ac 805c 7dc1 019e 8453 7c10 0070
+0158: 840c 7dc1 0166 880c 7dc1 0175 8c0c 7dc1
+0160: 018b 900c 7dc1 0184 7dc1 0152 8001 7c10
+0168: 00ac 89e3 008d 8801 7c10 00ac 841c 7dc1
+0170: 014f 89e2 008d 7dc1 014f 8001 7c10 00ac
+0178: 89e2 008d 8801 7c10 00ac 841c 7dc1 014f
+0180: 89e3 008d 7dc1 014f 7de1 01c9 0064 7c51
+0188: 0064 7dc1 0152 8001 7c10 00ac 85e2 008b
+0190: 8de9 008b 8801 7c10 00ac 841c 7dc1 014f
+0198: 85e3 008b 8de9 008b 7dc1 014f 8001 7c10
+01a0: 00ac 7de2 008d 0020 8801 7c10 00ac 841c
+01a8: 7dc1 014d 7de3 008d 0020 8401 7c10 00ac
+01b0: 7c10 00f2 7dc1 013e 7c21 f480 7c01 8089
+01b8: 7c11 021e 7c10 005f 7c01 80a9 7c11 022d
+01c0: 7c10 005f 7c01 80c9 7c11 021e 7c10 005f
+01c8: 85c3 09c4 09c4 09c4 07d0 05dc 03e8 0320
+01d0: 028a 01f4 012c 00c8 0064 004b 0032 001e
+01d8: 0014 000a 0005 0003 0002 0001 0000 0002
+01e0: 0005 000f 003c 0000 0001 0000 0050 0072
+01e8: 0065 0073 0073 0020 0061 006e 0079 0020
+01f0: 006b 0065 0079 0020 0074 006f 0000 0020
+01f8: 006d 0061 006b 0065 0020 0067 0061 006d
+0200: 0065 0020 0072 0061 006e 0064 006f 006d
+0208: 003a 0000 0053 0063 006f 0072 0065 003a
+0210: 0000 004c 0065 0076 0065 006c 003a 0000
+0218: 004e 0065 0078 0074 003a 0000 0020 0020
+0220: 0020 0020 0020 0020 0020 0020 0020 0020
+0228: 0020 0020 0020 0020 0000 0020 0020 0047
+0230: 0061 006d 0065 0020 004f 0076 0065 0072
+0238: 0021 0020 0020 0000 0000 0c00 0000 0000
+0240: 0000 0c00 0000 0000 0000 0c00 0000 0000
+0248: 0000 0c00 0000 0000 0000 0000 0000 0000
+0250: 0c00 0c00 0c00 0c00 0000 0000 0000 0000
+0258: 0000 0000 0000 0000 0000 0c00 0000 0000
+0260: 0000 0c00 0000 0000 0000 0c00 0000 0000
+0268: 0000 0c00 0000 0000 0000 0000 0000 0000
+0270: 0c00 0c00 0c00 0c00 0000 0000 0000 0000
+0278: 0000 0000 0000 0000 0d00 0000 0000 0000
+0280: 0d00 0d00 0d00 0000 0000 0000 0000 0000
+0288: 0000 0000 0000 0000 0000 0d00 0d00 0000
+0290: 0000 0d00 0000 0000 0000 0d00 0000 0000
+02a0: 0d00 0d00 0d00 0000 0000 0000 0d00 0000
+02a8: 0000 0000 0000 0000 0000 0d00 0000 0000
+02b0: 0000 0d00 0000 0000 0d00 0d00 0000 0000
+02b8: 0000 0000 0000 0000 0000 0000 0e00 0000
+02c0: 0e00 0e00 0e00 0000 0000 0000 0000 0000
+02c8: 0000 0000 0000 0000 0000 0e00 0000 0000
+02d0: 0000 0e00 0000 0000 0000 0e00 0e00 0000
+02e0: 0e00 0e00 0e00 0000 0e00 0000 0000 0000
+02e8: 0000 0000 0000 0000 0e00 0e00 0000 0000
+02f0: 0000 0e00 0000 0000 0000 0e00 0000 0000
+02f8: 0000 0000 0000 0000 0b00 0b00 0000 0000
+0300: 0b00 0b00 0000 0000 0000 0000 0000 0000
+0308: 0000 0000 0000 0000 0b00 0b00 0000 0000
+0310: 0b00 0b00 0000 0000 0000 0000 0000 0000
+0318: 0000 0000 0000 0000 0b00 0b00 0000 0000
+0320: 0b00 0b00 0000 0000 0000 0000 0000 0000
+0328: 0000 0000 0000 0000 0b00 0b00 0000 0000
+0330: 0b00 0b00 0000 0000 0000 0000 0000 0000
+0338: 0000 0000 0000 0000 0000 0900 0900 0000
+0340: 0900 0900 0000 0000 0000 0000 0000 0000
+0348: 0000 0000 0000 0000 0000 0900 0000 0000
+0350: 0000 0900 0900 0000 0000 0000 0900 0000
+0358: 0000 0000 0000 0000 0000 0900 0900 0000
+0360: 0900 0900 0000 0000 0000 0000 0000 0000
+0368: 0000 0000 0000 0000 0000 0900 0000 0000
+0370: 0000 0900 0900 0000 0000 0000 0900 0000
+0378: 0000 0000 0000 0000 0800 0800 0800 0000
+0380: 0000 0800 0000 0000 0000 0000 0000 0000
+0388: 0000 0000 0000 0000 0000 0800 0000 0000
+0390: 0800 0800 0000 0000 0000 0800 0000 0000
+0398: 0000 0000 0000 0000 0000 0800 0000 0000
+03a0: 0800 0800 0800 0000 0000 0000 0000 0000
+03a8: 0000 0000 0000 0000 0000 0800 0000 0000
+03b0: 0000 0800 0800 0000 0000 0800 0000 0000
+03b8: 0000 0000 0000 0000 0a00 0a00 0000 0000
+03c0: 0000 0a00 0a00 0000 0000 0000 0000 0000
+03c8: 0000 0000 0000 0000 0000 0000 0a00 0000
+03d0: 0000 0a00 0a00 0000 0000 0a00 0000 0000
+03d8: 0000 0000 0000 0000 0a00 0a00 0000 0000
+03e0: 0000 0a00 0a00 0000 0000 0000 0000 0000
+03e8: 0000 0000 0000 0000 0000 0000 0a00 0000
+03f0: 0000 0a00 0a00 0000 0000 0a00 0000 0000
diff --git a/examples/Tetris.dasm16 b/examples/Tetris.dasm16
new file mode 100644
index 0000000..fc7cb21
--- /dev/null
+++ b/examples/Tetris.dasm16
@@ -0,0 +1,424 @@
+; retrieve some entropy...
+ SET A, 0x8000 + 32 * 1 + 2
+ SET B, entropy_str1
+ JSR print
+ SET A, 0x8000 + 32 * 2 + 2
+ SET B, entropy_str2
+ JSR print
+
+ SET I, 0
+:cyc4
+ ADD I, 1
+ JSR next_key
+ IFE A, 0
+ SET PC, cyc4
+ MUL [rand_seed], 36313
+ ADD [rand_seed], A
+ MUL [rand_seed], 36313
+ ADD [rand_seed], I
+
+ SET A, 0x8000 ; clear screen
+:cyc3
+ SET [A], 0
+ ADD A, 1
+ IFG 0x8000 + 32*12, A
+ SET PC, cyc3
+
+; init
+ SET A, 0x8000
+ SET B, 0
+:cyc1
+ SET [A], 0x0700
+ ADD A, 1
+ ADD B, 1
+ IFG 10, B
+ SET PC, jmp1
+ SET B, 0
+ ADD A, 32 - 10
+:jmp1
+ IFG 0x8000 + 32*12, A
+ SET PC, cyc1
+
+ SET C, 0xf700
+ SET A, 0x8000 + 32 * 1 + 1 ; (1, 1)
+ SET B, score_str
+ JSR print
+ SET A, 0x8000 + 32 * 3 + 1 ; (1, 3)
+ SET B, level_str
+ JSR print
+ SET A, 0x8000 + 32 * 5 + 1 ; (1, 5)
+ SET B, next_str
+ JSR print
+ SET A, 0x8000
+:cyc2
+ SET [A + 10], 0x0100
+ SET [A + 31], 0x0100
+ ADD A, 32
+ IFG 0x8000 + 32*12, A
+ SET PC, cyc2
+
+ JSR update_score
+ JSR update_level
+ JSR select_next_piece
+
+SET PC, main_loop
+
+;;;;;;;;;;;;;;;
+; SUBROUTINES ;
+;;;;;;;;;;;;;;;
+
+:print ; takes coord at A, string to output at B, style at C
+ IFE [B], 0
+ SET PC, POP
+ SET [A], [B]
+ BOR [A], C
+ ADD A, 1
+ ADD B, 1
+ SET PC, print
+
+:next_rand ; takes no arguments, returns random word in A
+ MUL [rand_seed], 10061
+ ADD [rand_seed], 1
+ SET A, [rand_seed]
+ SET PC, POP
+:rand_seed
+ DAT 0xC00F ; TODO: collect entropy at start to randomize game
+
+:next_key ; reads next key to A from keyboard (based on Notch's code)
+ SET B, [keypointer]
+ ADD B, 0x9000
+ SET A, [B]
+ IFE A, 0
+ SET PC, POP
+ SET [B], 0
+ ADD [keypointer], 1
+ AND [keypointer], 15
+ SET PC, POP
+:keypointer
+ DAT 0
+
+:select_next_piece ; selects next piece
+ JSR next_rand
+ MOD A, 7
+ SET [cur_piece], [next_piece]
+ SET [next_piece], A
+ SET [piece_pos], 0x8000 + 17
+ SET PC, POP
+:cur_piece
+ DAT 0
+:cur_rot
+ DAT 0
+:next_piece
+ DAT 0
+:piece_pos
+ DAT 0x8000 + 17
+
+:update_score ; display current score
+ SET A, [score]
+ SET C, 0x8000 + 32*2 + 8
+:score_cyc1
+ SET B, A
+ MOD B, 10
+ BOR B, 0xe730
+ SET [C], B
+ SUB C, 1
+ DIV A, 10
+ IFG A, 0
+ SET PC, score_cyc1
+ SET PC, POP
+
+:update_level ; display current level
+ SET A, [level]
+ SET C, 0x8000 + 32*4 + 8
+:level_cyc1
+ SET B, A
+ MOD B, 10
+ BOR B, 0xe730
+ SET [C], B
+ SUB C, 1
+ DIV A, 10
+ IFG A, 0
+ SET PC, level_cyc1
+ SET PC, POP
+
+:show_cur_piece ; display/clear/check current piece (A=0 - clear, A=1 - display, A=2 - check), if A=2 doesn't actually place anything, return B=1 is position is valid, 0 otherwise
+ SET X, [piece_pos] ; place block at [X] (display)
+ SET Y, [cur_piece] ; ...from [Y] (pieces array)
+ SHL Y, 2
+ BOR Y, [cur_rot]
+ SHL Y, 4
+ ADD Y, pieces
+ SET I, 0 ; index
+:piece_cyc1
+ SET B, [Y]
+ IFE B, 0
+ SET PC, piece_jmp1
+ IFG 2, A
+ SET PC, piece_jmp2
+ IFG X, 0x8000 + 32*12
+ ADD PC, 3
+ IFE [X], 0
+ SET PC, piece_jmp1
+ SET B, 0
+ SET PC, POP
+:piece_jmp2
+ IFE A, 0
+ SET B, 0
+ SET [X], B
+ SET [X + 1], B
+:piece_jmp1
+ ADD I, 1
+ ADD X, 2
+ ADD Y, 1
+ SET B, 1
+ IFE I, 16
+ SET PC, POP
+ IFB I, 3
+ SET PC, piece_cyc1
+ ADD X, 32 - 8
+ SET PC, piece_cyc1
+
+:show_next_piece ; redraw next piece
+ SET X, 0x8000 + 32*7 + 1 ; place block at [X] (display)
+ SET Y, [next_piece] ; ...from [Y] (pieces array)
+ SHL Y, 6
+ ADD Y, pieces
+ SET I, 0 ; index
+:npiece_cyc1
+ SET B, [Y]
+ IFE B, 0
+ SET B, 0x0700
+ SET [X], B
+ SET [X + 1], B
+:npiece_jmp1
+ ADD I, 1
+ ADD X, 2
+ ADD Y, 1
+ SET B, 1
+ IFE I, 16
+ SET PC, POP
+ IFB I, 3
+ SET PC, npiece_cyc1
+ ADD X, 32 - 8
+ SET PC, npiece_cyc1
+
+:scan_lines ; search for complete lines, remove them and move all other down; update score & level
+ SET A, 0x8000 + 32*11 + 11 ; start of next line to fill
+ SET B, A ; start of next line to check
+ SET J, 0 ; num of lines skipped
+:scan_cyc2
+ SET I, 0 ; horizontal index
+ SET X, B
+:scan_cyc1
+ IFE [X], 0
+ SET PC, scan_jmp1
+ ADD X, 2
+ ADD I, 1
+ IFG 10, I
+ SET PC, scan_cyc1
+ ADD J, 1 ; no gaps found, increase num of complete rows
+ SUB B, 32
+ IFE J, 4
+ SET PC, scan_jmp1
+ IFG B, 0x8000
+ SET PC, scan_cyc2
+:scan_jmp1 ; found a gap, or no more gaps can be found
+ IFE A, B
+ SET PC, scan_jmp2 ; no need to move anything, continue
+ SET I, 0
+:scan_cyc3
+ SET [A], [B]
+ ADD I, 1
+ ADD A, 1
+ ADD B, 1
+ IFG 20, I
+ SET PC, scan_cyc3
+ SUB A, 20
+ SUB B, 20
+:scan_jmp2
+ SUB A, 32
+ IFG 0x8000, A
+ SET PC, scan_end
+ SUB B, 32
+ IFE J, 4
+ SET PC, scan_jmp1
+ IFG 0x8000, B
+ SET PC, scan_jmp1
+ SET PC, scan_cyc2
+:scan_end
+ IFE J, 0
+ SET PC, POP
+ ADD [lines], J
+ SET J, [lines_score + J]
+ MUL J, [level]
+ ADD [score], J
+ JSR update_score
+ IFG 10, [lines]
+ SET PC, POP
+ SET [lines], 0
+ ADD [level], 1
+ JSR update_level
+ SET PC, POP
+
+:main_loop
+ JSR select_next_piece
+ SET A, 2
+ JSR show_cur_piece ; check if we can drop next piece
+ IFE B, 0
+ SET PC, game_over
+ JSR show_next_piece ; redraw next piece
+ SET A, [level]
+ SET [cycle_num], [level_cycles + A]
+
+:drop_loop
+ SET Z, [cycle_num]
+:wait_loop_redraw ; "heavy" way, redraw current piece
+ SET A, 1
+ JSR show_cur_piece
+:wait_loop ; "light" way, when no keys were pressed
+ IFE Z, 0
+ SET PC, drop_jmp
+ SUB Z, 1
+; read from keyboard
+ JSR next_key
+ IFE A, 1
+ SET PC, key_left
+ IFE A, 2
+ SET PC, key_right
+ IFE A, 3
+ SET PC, key_up
+ IFE A, 4
+ SET PC, key_down
+ SET PC, wait_loop
+
+:key_left
+ SET A, 0
+ JSR show_cur_piece
+ SUB [piece_pos], 2
+ SET A, 2
+ JSR show_cur_piece
+ IFE B, 1
+ SET PC, wait_loop_redraw
+ ADD [piece_pos], 2
+ SET PC, wait_loop_redraw
+
+:key_right
+ SET A, 0
+ JSR show_cur_piece
+ ADD [piece_pos], 2
+ SET A, 2
+ JSR show_cur_piece
+ IFE B, 1
+ SET PC, wait_loop_redraw
+ SUB [piece_pos], 2
+ SET PC, wait_loop_redraw
+
+:key_down
+ SET [cycle_num], 100
+ SET Z, 100
+ SET PC, wait_loop
+
+:key_up
+ SET A, 0
+ JSR show_cur_piece
+ ADD [cur_rot], 1
+ AND [cur_rot], 3
+ SET A, 2
+ JSR show_cur_piece
+ IFE B, 1
+ SET PC, wait_loop_redraw
+ SUB [cur_rot], 1
+ AND [cur_rot], 3
+ SET PC, wait_loop_redraw
+
+
+:drop_jmp
+; lower current piece
+ SET A, 0
+ JSR show_cur_piece
+ ADD [piece_pos], 32
+ SET A, 2
+ JSR show_cur_piece
+ IFE B, 1
+ SET PC, drop_loop
+ SUB [piece_pos], 32
+ SET A, 1
+ JSR show_cur_piece
+ JSR scan_lines
+ SET PC, main_loop
+
+:game_over ; print blinking game over message
+ SET C, 0xf480
+ SET A, 0x8000 + 32 * 4 + 9 ; (9, 4)
+ SET B, game_over_pad
+ JSR print
+ SET A, 0x8000 + 32 * 5 + 9 ; (9, 5)
+ SET B, game_over_str
+ JSR print
+ SET A, 0x8000 + 32 * 6 + 9 ; (9, 6)
+ SET B, game_over_pad
+ JSR print
+ SUB PC, 1
+
+:cycle_num
+ DAT 2500
+:level_cycles
+ DAT 2500, 2500, 2000, 1500, 1000, 800, 650, 500, 300, 200, 100, 75, 50, 30, 20, 10, 5, 3, 2, 1
+:lines_score
+ DAT 0, 2, 5, 15, 60
+:lines
+ DAT 0
+:level
+ DAT 1
+:score
+ DAT 0
+:entropy_str1
+ DAT "Press any key to", 0
+:entropy_str2
+ DAT " make game random:", 0
+:score_str
+ DAT "Score:", 0
+:level_str
+ DAT "Level:", 0
+:next_str
+ DAT "Next:", 0
+:game_over_pad
+ DAT " ", 0
+:game_over_str
+ DAT " Game Over! ", 0
+:pieces
+; I
+ DAT 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0
+ DAT 0, 0, 0, 0, 0x0c00, 0x0c00, 0x0c00, 0x0c00, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0, 0, 0x0c00, 0, 0
+ DAT 0, 0, 0, 0, 0x0c00, 0x0c00, 0x0c00, 0x0c00, 0, 0, 0, 0, 0, 0, 0, 0
+; J
+ DAT 0x0d00, 0, 0, 0, 0x0d00, 0x0d00, 0x0d00, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0d00, 0x0d00, 0, 0, 0x0d00, 0, 0, 0, 0x0d00, 0, 0, 0, 0, 0, 0
+ DAT 0, 0, 0, 0, 0x0d00, 0x0d00, 0x0d00, 0, 0, 0, 0x0d00, 0, 0, 0, 0, 0
+ DAT 0, 0x0d00, 0, 0, 0, 0x0d00, 0, 0, 0x0d00, 0x0d00, 0, 0, 0, 0, 0, 0
+; L
+ DAT 0, 0, 0x0e00, 0, 0x0e00, 0x0e00, 0x0e00, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0e00, 0, 0, 0, 0x0e00, 0, 0, 0, 0x0e00, 0x0e00, 0, 0, 0, 0, 0
+ DAT 0, 0, 0, 0, 0x0e00, 0x0e00, 0x0e00, 0, 0x0e00, 0, 0, 0, 0, 0, 0, 0
+ DAT 0x0e00, 0x0e00, 0, 0, 0, 0x0e00, 0, 0, 0, 0x0e00, 0, 0, 0, 0, 0, 0
+; O
+ DAT 0x0b00, 0x0b00, 0, 0, 0x0b00, 0x0b00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0x0b00, 0x0b00, 0, 0, 0x0b00, 0x0b00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0x0b00, 0x0b00, 0, 0, 0x0b00, 0x0b00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0x0b00, 0x0b00, 0, 0, 0x0b00, 0x0b00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; S
+ DAT 0, 0x0900, 0x0900, 0, 0x0900, 0x0900, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0900, 0, 0, 0, 0x0900, 0x0900, 0, 0, 0, 0x0900, 0, 0, 0, 0, 0
+ DAT 0, 0x0900, 0x0900, 0, 0x0900, 0x0900, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0900, 0, 0, 0, 0x0900, 0x0900, 0, 0, 0, 0x0900, 0, 0, 0, 0, 0
+; T
+ DAT 0x0800, 0x0800, 0x0800, 0, 0, 0x0800, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0800, 0, 0, 0x0800, 0x0800, 0, 0, 0, 0x0800, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0800, 0, 0, 0x0800, 0x0800, 0x0800, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0x0800, 0, 0, 0, 0x0800, 0x0800, 0, 0, 0x0800, 0, 0, 0, 0, 0, 0
+; Z
+ DAT 0x0a00, 0x0a00, 0, 0, 0, 0x0a00, 0x0a00, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0, 0x0a00, 0, 0, 0x0a00, 0x0a00, 0, 0, 0x0a00, 0, 0, 0, 0, 0, 0
+ DAT 0x0a00, 0x0a00, 0, 0, 0, 0x0a00, 0x0a00, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ DAT 0, 0, 0x0a00, 0, 0, 0x0a00, 0x0a00, 0, 0, 0x0a00, 0, 0, 0, 0, 0, 0 \ No newline at end of file
diff --git a/examples/alpha.s b/examples/alpha.s
new file mode 100644
index 0000000..5504310
--- /dev/null
+++ b/examples/alpha.s
@@ -0,0 +1,19 @@
+SET PUSH, 0x0000
+:LABEL0BEGIN_WHILE
+IFG 0x001A, PEEK
+SET PC, LABEL1ELSE
+SET PC, LABEL2END
+:LABEL1ELSE
+SET B, PEEK
+SET A, 0x8000
+ADD A, B
+SET C, PEEK
+SET B, 0x0041
+ADD B, C
+SET [A], B
+SET A, PEEK
+ADD A, 0x0001
+SET PEEK, A
+SET PC, LABEL0BEGIN_WHILE
+:LABEL2END
+BRK
diff --git a/examples/brk.s b/examples/brk.s
new file mode 100644
index 0000000..55c156c
--- /dev/null
+++ b/examples/brk.s
@@ -0,0 +1 @@
+brk
diff --git a/examples/colour_cycle.s b/examples/colour_cycle.s
new file mode 100644
index 0000000..016a2a2
--- /dev/null
+++ b/examples/colour_cycle.s
@@ -0,0 +1,15 @@
+;Testing the display memory, and making pretty colours.
+; By SmokestormX
+set z,0x8000; Starting mem
+set c,0x0080; Blank Char
+set i,0;
+:loop
+set [0x8000+i],c; Set the Display+offset to C
+add i,1;
+ife i,0x200; 0x200 is the exact size of the display.
+set pc,end;
+set pc,loop;
+:end
+set i,0;
+add c,0x0100; Add offset to C , change this around.
+set pc,loop;
diff --git a/examples/dcpu.core b/examples/dcpu.core
new file mode 100644
index 0000000..2a5db28
--- /dev/null
+++ b/examples/dcpu.core
Binary files differ
diff --git a/examples/disp.s b/examples/disp.s
new file mode 100644
index 0000000..a516f23
--- /dev/null
+++ b/examples/disp.s
@@ -0,0 +1,14 @@
+;Testing the display memory, and making pretty colours.
+; By SmokestormX
+set z,0x8000; Starting mem
+set c,0x1041; Yellow A charachter
+set i,0;
+:loop
+set [0x8000+i],c; Set the Display+offset to C
+add i,1;
+add c,0x1100; Add offset to C , change this around.
+ife i,0x200; 0x200 is the exact size of the display.
+set pc,end;
+set pc,loop;
+:end
+brk
diff --git a/examples/hello.asm b/examples/hello.asm
new file mode 100644
index 0000000..1b3897a
--- /dev/null
+++ b/examples/hello.asm
@@ -0,0 +1,18 @@
+; Assembler test for DCPU
+; by Markus Persson
+
+ set a, 0xbeef ; Assign 0xbeef to register a
+ set [0x1000], a ; Assign memory at 0x1000 to value of register a
+ ifn a, [0x1000] ; Compare value of register a to memory at 0x1000 ..
+ set PC, end ; .. and jump to end if they don't match
+
+ set i, 0 ; Init loop counter, for clarity
+:nextchar ife [data+i], 0 ; If the character is 0 ..
+ set PC, end ; .. jump to the end
+ set [0x8000+i], [data+i] ; Video ram starts at 0x8000, copy char there
+ add i, 1 ; Increase loop counter
+ set PC, nextchar ; Loop
+
+:data dat "Hello world!", 0 ; Zero terminated string
+
+:end sub PC, 1 ; Freeze the CPU forever \ No newline at end of file
diff --git a/examples/hello2.asm b/examples/hello2.asm
new file mode 100644
index 0000000..9bb5e9a
--- /dev/null
+++ b/examples/hello2.asm
@@ -0,0 +1,37 @@
+; Notch's second "hello word" program.
+; http://i.imgur.com/XIXc4.jpg
+; Supposed to show formatting.
+:start
+ set i, 0
+ set j, 0
+ set b, 0xf100
+:nextchar
+ set a, [data+i]
+ ife a, 0
+ set PC, end
+ ifg a, 0xff
+ set PC, setcolor
+ bor a, b
+ set [0x8000+j], a
+ add i, 1
+ add j, 1
+ set PC, nextchar
+:setcolor
+ set b, a
+ and b, 0xff
+ shl b, 8
+ ifg a, 0x1ff
+ add b, 0x80 ; Add high bit to each character we color. So the character must be 7-bit ASCII
+ add i, 1
+ set PC, nextchar
+:data
+ dat 0x170, "Hello ", 0x2e1, "world", 0x170, ", how are you?", 0
+ ; Color format:
+ ; After processing:
+ ; 0x170 -> b = 0x7000 -> 0111 0000 0XXX XXXX = white(grey) on black
+ ; 0x2e1 -> b = 0xe180 -> 1110 0001 1XXX XXXX = yellow on blue
+ ; b gets OR'd with each character.
+ ; ANSI says: black is 0, white is 7, yellow is 3, blue is 4
+ ; If black is 0 and grey is 7, it's <FORE> <BACK> <EXTRA BIT> or <FORE> <EXTRA BIT> <BACK>
+:end
+ set PC, start
diff --git a/examples/ique_fibonacci.asm b/examples/ique_fibonacci.asm
new file mode 100644
index 0000000..a7f13e4
--- /dev/null
+++ b/examples/ique_fibonacci.asm
@@ -0,0 +1,12 @@
+; http://www.reddit.com/r/dcpu_16_programming/comments/rtvnp/i_wrote_a_program_that_fills_up_the_memory_with/
+
+SET A, 1
+SET B, 1
+SET PEEK, 1
+:loop ADD A, B ; A is now 2, B is still 1
+SET PUSH, A
+SET A, B
+SET B, PEEK
+IFG SP, 10 ; 10 because that's how much space
+ ; this program takes
+SET PC, loop \ No newline at end of file
diff --git a/examples/keyb.s b/examples/keyb.s
new file mode 100644
index 0000000..3871a1d
--- /dev/null
+++ b/examples/keyb.s
@@ -0,0 +1,12 @@
+ set a, 0
+ set b, 0
+ set c, 0xf000
+:loop
+ ife [0x9000], 0
+ set pc, loop
+ set b, [0x9000]
+ bor b, c
+ set [0x8000 + a], b
+ set [0x9000], 0
+ add a, 1
+ set pc, loop
diff --git a/examples/keyboard_reader.asm b/examples/keyboard_reader.asm
new file mode 100644
index 0000000..9c7e88f
--- /dev/null
+++ b/examples/keyboard_reader.asm
@@ -0,0 +1,20 @@
+; Reading characters from the keyboard
+; by Markus Persson
+
+#macro nextkey(target) {
+ push(i)
+ set i,[keypointer]
+ add i,0x9000
+ set target,[i]
+ ife target,0
+ jmp end
+
+ set [i],0
+ add [keypointer], 1
+ and [keypointer], 0xf
+:end
+ pop(i)
+}
+
+:keypointer
+dat 0
diff --git a/examples/matrix.s b/examples/matrix.s
new file mode 100644
index 0000000..e83d1dc
--- /dev/null
+++ b/examples/matrix.s
@@ -0,0 +1,114 @@
+ set z, 0x1234 ; rand_seed
+set y, z ; cur_rand
+set pc, main_loop
+
+:next_rand
+mul y, 10061
+add y, 1
+set pc, pop
+
+:main_loop
+set i, 0
+set x, y
+set y, z
+set a, 0x8000
+
+:next_row1
+ set j, 0
+ :next_char1
+ jsr next_rand
+ ife [a], 0
+ set pc, skip1
+ ifb y, 0x7000
+ set pc, skip1
+
+ ; mutate char at [a] (j, i)
+ set push, y
+ set y, x
+ jsr next_rand
+ and [a], 0xff00 ; reset char
+ and y, 0x003f
+ bor [a], [code+y] ; set letter
+ set x, y
+ set y, pop
+
+ :skip1
+ add j, 1
+ add a, 1
+ ifg 32, j
+ set pc, next_char1
+ add i, 1
+ ifg 12, i
+ set pc, next_row1
+set y, x
+
+; step 2: move all columns down
+set i, 12
+set a, 0x817f
+
+:next_row2
+ set j, 32
+ :next_char2
+ ife [a], 0
+ set pc, empty_char
+ ifb [a+32], 0xffff
+ set pc, move_down
+
+ ; add new char at [a+32] (j, i+1)
+ jsr next_rand
+ set [a+32], [a]
+ and [a+32], 0xff00
+ set x, y
+ and x, 0x003f
+ bor [a+32], [code+x]
+ and [a], 0x7fff
+ set pc, skip2
+
+ :empty_char
+ set [a+32], 0
+
+ :move_down
+ and [a+32], 0x7fff
+
+ :skip2
+ sub j, 1
+ sub a, 1
+ ifg j, 0
+ set pc, next_char2
+ sub i, 1
+ ifg i, 0
+ set pc, next_row2
+set y, x
+
+; step 3: update top layer
+set a, 0x8000
+set j, 0
+:next_char3
+ jsr next_rand
+ ifb y, 0x0700
+ set pc, skip3
+ ifb [a], 0xffff
+ set pc, empty_char2
+
+ set [a], 0x2000
+ ifb y, 0x0800
+ set [a], 0xa000
+ set x, y
+ and x, 0x003f
+ bor [a], [code+x]
+ set pc, skip3
+
+ :empty_char2
+ set [a], 0
+
+ :skip3
+ add j, 1
+ add a, 1
+ ifg 32, j
+ set pc, next_char3
+
+set PC, main_loop
+
+sub PC, 1
+
+:code dat "00112334567889&||!!@==::**##<>>__TYYUDQZJJIX- ~~oiwlrkm//\\'[]^)`"
diff --git a/examples/matrix2.s b/examples/matrix2.s
new file mode 100644
index 0000000..04da5fe
--- /dev/null
+++ b/examples/matrix2.s
@@ -0,0 +1,276 @@
+;you may need to fix colors if your emulator does
+;some other scheme, search 0xF200 and 0x0200 and
+;change those values.
+;
+;you can also adjust display_width and display_height
+;if your emulator differs from 32x12.
+;
+; ~enjoy~
+;
+;(ps, feel free to create an optimized version)
+
+:entrypoint
+ set a, 12345 ;change me!
+ jsr srand
+ jsr clear_screen
+ jsr init_leading
+ jsr init_tail_length
+ jsr init_speed
+ set pc, main ;never returns
+
+:display_width
+ dat 32 ;max 32
+:display_height
+ dat 16 ;max 16
+
+:main
+ set push, x
+ set push, y
+ set push, z
+ set i, 0
+
+:main_loop_start
+ set x, 0
+ add i, 1
+ mod i, 40
+ set pc, main_loop
+
+:main_next
+ add x, 1
+ ife x, [display_width]
+ set pc, main_loop_start
+
+:main_loop
+ set a, i
+ mod a, [speed+x]
+ ifn a, 0
+ set pc, main_next
+
+ ;clear tail
+ set y, [leading+x]
+ sub y, [tail_length+x]
+ ifg y, 0x7FFF
+ add y, [display_height]
+ set c, 0x0020
+ set b, y
+ set a, x
+ jsr set_vram
+
+ ;unhighlight last character
+ set y, [leading+x]
+ sub y, 1
+ ifg y, 0x7FFF
+ add y, [display_height]
+ set b, y
+ set a, x
+ jsr get_vram
+ set c, a
+ and c, 0x00FF
+ bor c, 0xF200
+ set b, y
+ set a, x
+ jsr set_vram
+
+ ;draw leading
+ jsr get_character
+ set c, a
+ bor c, 0x0200
+ set b, [leading+x]
+ set a, x
+ jsr set_vram
+
+ ;move ahead
+ set a, [leading+x]
+ add a, 1
+ mod a, [display_height]
+ set [leading+x], a
+
+ set pc, main_next
+
+:get_vram
+ mul b, [display_width]
+ add a, b
+ add a, 0x8000
+ set a, [a]
+ set pc, pop
+
+:set_vram
+ mul b, [display_width]
+ add a, b
+ add a, 0x8000
+ set [a], c
+ set pc, pop
+
+:leading
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+:leading_end
+
+:tail_length
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+:tail_length_end
+
+:speed
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+ dat 0, 0, 0, 0, 0, 0, 0, 0
+:speed_end
+
+:init_leading
+ set push, i
+ set push, y
+ set y, [display_height]
+ set i, leading
+:init_leading_loop
+ jsr rand
+ mod a, y
+ set [i], a
+ add i, 1
+ ifn i, leading_end
+ set pc, init_leading_loop
+ set y, pop
+ set i, pop
+ set pc, pop
+
+:init_tail_length
+ set push, i
+ set push, y
+ set y, [display_height]
+ div y, 3
+ set i, tail_length
+:init_tail_length_loop
+ jsr rand
+ mod a, y
+ add a, 2
+ set [i], a
+ add i, 1
+ ifn i, tail_length_end
+ set pc, init_tail_length_loop
+ set y, pop
+ set i, pop
+ set pc, pop
+
+:init_speed
+ set push, i
+ set i, speed
+:init_speed_loop
+ jsr rand
+ mod a, 35
+ add a, 5
+ set [i], a
+ add i, 1
+ ifn i, speed_end
+ set pc, init_speed_loop
+ set i, pop
+ set pc, pop
+
+:srand
+ set [rand_seed], a
+ set pc, pop
+
+:rand
+ set a, [rand_seed]
+ mul a, 0x7C4D
+ add a, 0x3619
+ set b, rand_seed
+ set [b], a
+ set pc, pop
+:rand_seed
+ dat 0x5555
+
+:get_character
+ jsr rand
+ mod a, 10
+ set b, [get_character_table+a]
+ set pc, b
+:get_character_table
+ dat get_character_number
+ dat get_character_number
+ dat get_character_number
+ dat get_character_lower
+ dat get_character_lower
+ dat get_character_upper
+ dat get_character_upper
+ dat get_character_special_1
+ dat get_character_special_2
+ dat get_character_special_3
+:get_character_number
+ jsr rand
+ mod a, 10
+ add a, 0x30
+ set pc, pop
+:get_character_lower
+ jsr rand
+ mod a, 27
+ add a, 0x61
+ set pc, pop
+:get_character_upper
+ jsr rand
+ mod a, 27
+ add a, 0x41
+ set pc, pop
+:get_character_special_1
+ jsr rand
+ mod a, 0x0F
+ add a, 0x21
+ set pc, pop
+:get_character_special_2
+ jsr rand
+ mod a, 0x05
+ add a, 0x5B
+ set pc, pop
+:get_character_special_3
+ jsr rand
+ mod a, 0x04
+ add a, 0x7B
+ set pc, pop
+
+:clear_screen
+ set push, j
+ set j, sp
+ set sp, 0x8200
+:clear_screen_loop
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ set push, 0x20
+ ifn 0x8000, sp
+ set pc, clear_screen_loop
+ set sp, j
+ set j, pop
+ set pc, pop
+
diff --git a/examples/matrix3.s b/examples/matrix3.s
new file mode 100644
index 0000000..5909188
--- /dev/null
+++ b/examples/matrix3.s
@@ -0,0 +1,325 @@
+SET PC, Init
+
+:Seed DAT 0x1234
+
+:WIDTH DAT 32;32
+:HEIGHT DAT 12; 12
+
+:CELL_COUNT DAT 384; 384
+;SCREEN DAT 0x8000
+
+:NSPINNERS DAT 2
+:NGLYPHS DAT 0x26
+:NGLYPHS1 DAT 0x25
+:CHAR_MAP
+ DAT 32
+:CHAR_MAP1
+ DAT 48,49,50,51,52,53,54,55,56,57,64
+ DAT 97,98,99,100,101,102,103,104,105
+ DAT 106,107,108,109,110,111,112,113
+ DAT 114,115,116,117,118,119,120,121
+ DAT 122
+:DENSITY
+ DAT 3
+
+;Feeders 0x7000
+;Cells 0x6000
+
+:Init
+ JSR InitSpinners
+ SET I, 0x7000 ;Feeders
+ SET J, I
+ ADD J, [WIDTH]
+ :InitLoop
+ SET [I], 0
+ ADD I, 1
+ IFN I, J
+ SET PC, InitLoop
+
+:MainLoop
+ ; generate
+ SET I, 0
+ :GenLoop
+ SET J, I
+ Add J, 0x7000
+ SET Z, [J]
+ SHR Z, 4
+ AND Z, 0xF
+ IFN Z, 0
+ SET PC, GenCase1
+ SET X, [J]
+ AND X, 0xF
+ SET Y, [J]
+ SHR Y, 8
+ IFG X, 0
+ SET PC, GenCase2
+ SET A, 0
+ SET B, I
+ SET C, Y
+ SET PUSH, I
+ JSR InsertGlyph
+ SET I, POP
+ SET X, [J]
+ AND X, 0xF
+ SET Y, [J]
+ SHR Y, 8
+ IFE Y, 0xF
+ SET PC, GenDone
+ ADD Y, 1
+ AND [J], 0xFF
+ SHL Y, 8
+ BOR [J], Y
+ SET PC, GenDone
+ :GenCase1
+ SUB Z, 1
+ SHL Z, 4
+ AND [J], 0xF0F
+ BOR [J], Z
+ SET PC, GenDone
+ :GenCase2
+ JSR Rnd
+ MOD A, [NGLYPHS]
+ SET B, I
+ SET C, Y
+ SET PUSH, I
+ JSR InsertGlyph
+ SET I, POP
+ SET X, [J]
+ AND X, 0xF
+ SET Y, [J]
+ SHR Y, 8
+ AND [J], 0xFF0
+ SUB X, 1
+ BOR [J], X
+ IFE Y, 0xF
+ SET PC, GenDone
+ ADD Y, 1
+ AND [J], 0xFF
+ SHL Y, 8
+ BOR [J], Y
+ :GenDone
+ JSR Rnd
+ MOD A, 10
+ IFN A, 0
+ SET PC, GenInc
+ JSR Rnd
+ MOD A, 5
+ SET B, A
+ JSR Rnd
+ MOD A, 5
+ ADD A, B
+ AND [J], 0xF0F
+ SHL A, 4
+ BOR [J], A
+ :GenInc
+ ADD I, 1
+ IFN I, [WIDTH]
+ SET PC, GenLoop
+ ; mutate
+ SET I, 0
+ :MLoop
+ SET X, I
+ ADD X, 0x7000
+ SET Y, [X]
+ SET A, Y
+ AND A, 0xFF
+ IFG A, 0
+ SET PC, MInc
+ JSR Rnd
+ MOD A, [DENSITY]
+ IFN A, 0
+ SET PC, MInc
+ JSR Rnd
+ MOD A, [HEIGHT]
+ ADD A, 3
+ SET [X], A
+ JSR Rnd
+ MOD A, 5
+ SET B, A
+ JSR Rnd
+ MOD A, 5
+ ADD A, B
+ SHL A, 4
+ BOR [X], A
+ JSR Rnd
+ MOD A, 4
+ IFN A, 0
+ AND [X], 0xF0
+ JSR Rnd
+ MOD A, 2
+ IFE A, 0
+ SET PC, MStream
+ SET A, [HEIGHT]
+ SHR A, 1
+ SET B, A
+ JSR Rnd
+ MOD A, B
+ SHL A, 8
+ BOR [X], A
+ SET PC, MInc
+ :MStream
+ BOR [X], 0xF00
+ :MInc
+ ADD I, 1
+ IFN I, [WIDTH]
+ SET PC, MLoop
+ JSR Rnd
+ MOD A, 500
+ IFE A, 0
+ JSR InitSpinners
+ ; draw
+ SET I, 0
+ :DrawLoop
+ SET X, I
+ MUL X, 2
+ ADD X, 0x6000 ; Cells
+ SET Y, X
+ ADD Y, 1
+ SET A, [Y]
+ AND A, 1
+ IFE A, 0
+ SET PC, DrawInc
+ SET B, [X]
+ SET C, B
+ AND B, 0xFF
+ SHR C, 8
+ IFE C, 0
+ SET PC, DrawBlank
+ SET J, 0x5000
+ IFN B, 0
+ SET J, 0x8000
+ IFB [Y], 2
+ SET J, 0x8000
+ SET Z, C
+ ADD Z, CHAR_MAP
+ SET Z, [Z]
+ BOR J, Z
+ SET Z, 0x8000
+ ADD Z, I
+ SET [Z], J
+ SET PC, DrawDone
+ :DrawBlank
+ SET Z, 0x8000
+ ADD Z, I
+ SET [Z], 0
+ :DrawDone
+ AND [Y], 2
+ IFE B, 0
+ SET PC, DrawSkipGlow
+ SUB B, 1
+ BOR [Y], 1
+ AND [X], 0xFF00
+ ADD [X], B
+ :DrawSkipGlow
+ IFG 2, [Y]
+ SET PC, DrawInc
+ JSR Rnd
+ MOD A, [NGLYPHS1]
+ ADD A, 1
+ SHL A, 8
+ AND [X], 0xFF
+ BOR [X], A
+ BOR [Y], 1
+ :DrawInc
+ ADD I, 1
+ IFN I, [CELL_COUNT]
+ SET PC, DrawLoop
+ SET PC, MainLoop
+
+:Rnd
+ SET A, [Seed]
+ MUL A, 0x7C4D
+ ADD A, 0x3619
+ SET [Seed], A
+ SET PC, POP
+
+:InitSpinners
+ SET C, 0x6000 ; Cells
+ SET I, C
+ SET X, [CELL_COUNT]
+ MUL X, 2
+ ADD X, C
+ :ISLoop1
+ SET [I], 0
+ ADD I, 1
+ SET [I], 0
+ ADD I, 1
+ IFN I, X
+ SET PC, ISLoop1
+ SET I, 0
+ SET PC, ISLoopTest
+ :ISLoop2
+ JSR Rnd
+ MOD A, [WIDTH]
+ SET B, A
+ JSR Rnd
+ MOD A, [HEIGHT]
+ MUL A, [WIDTH]
+ ADD B, A
+ MUL B, 2
+ ADD B, 1
+ ADD B, C
+ BOR [B], 2
+ ADD I, 1
+ :ISLoopTest
+ IFN I, [NSPINNERS]
+ SET PC, ISLoop2
+ SET PC, POP
+
+:InsertGlyph ; A = glyph, B = x, C = y
+ SET Z, 1
+ IFE C, 0xF
+ SET Z, 0
+ IFE Z, 0
+ SET C, 0
+ SET Y, [HEIGHT]
+ ADD Y, 1
+ IFG C, Y
+ SET PC, POP
+ IFE Z, 1
+ SET PC, IGSimple
+ SET I, [HEIGHT]
+ SUB I, 1
+ :IGLoop
+ SET Y, I
+ MUL Y, [WIDTH]
+ ADD Y, B
+ MUL Y, 2
+ ADD Y, 0x6000 ; Cells
+ SET X, Y
+ SUB X, [WIDTH]
+ SUB X, [WIDTH]
+ SET [Y], [X]
+ SET X, Y
+ ADD X, 1
+ BOR [X], 1
+ SUB I, 1
+ IFN I, 0
+ SET PC, IGLoop
+ SET Y, B
+ MUL Y, 2
+ ADD Y, 0x6000 ; Cells
+ SET PC, IGFinal
+ :IGSimple
+ SET Y, [WIDTH]
+ MUL Y, C
+ ADD Y, B
+ MUL Y, 2
+ ADD Y, 0x6000 ; Cells
+ :IGFinal
+ SHL A, 8
+ AND [Y], 0xFF
+ BOR [Y], A
+ SET X, Y
+ ADD X, 1
+ BOR [X], 1
+ IFE A, 0
+ SET PC, POP
+ AND [Y], 0xFF00
+ IFE Z, 0
+ SET PC, POP
+ JSR Rnd
+ MOD A, 2
+ ADD A, 1
+ BOR [Y], A
+ SET PC, POP
diff --git a/examples/minesweeper.s b/examples/minesweeper.s
new file mode 100644
index 0000000..b3c5f52
--- /dev/null
+++ b/examples/minesweeper.s
@@ -0,0 +1,1272 @@
+; MINESWEEPER by RHY3756547
+;
+; My first DCPU-16 project
+; Feel free to fork and port for operating systems.
+; Heavily commented. :>
+;
+; Twitter: #RHY3756547
+
+; ARROW KEYS to move, ENTER to select.
+; F to toggle flag on location.
+
+; Upon winning or losing, press enter to start again.
+
+JSR intro
+
+SET A, tile0
+SET B, 89
+SET C, 0x8182
+JSR storetiles
+:restart
+SET [gamestate], 0
+SET [gametimer], 0
+SET [game_started], 0
+SET [old_select_x], 10
+SET [old_select_y], 10
+JSR clear_board
+SET A, boardtiles_l1
+SET B, 384
+SET C, 0x8000
+JSR drawtiles
+
+SET A, minestext
+SET B, 2
+SET C, 0x8122
+SET Y, 0x8F00
+JSR drawtext
+JSR draw_timer
+
+JSR change_selection
+SET PC, gameloop
+
+:gametimer DAT 0
+:timer_second_value DAT 575
+:youwintext DAT 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055
+:youlosetext DAT 0x004F, 0x0050, 0x0051, 0x0056, 0x0057, 0x0058, 0x0059
+:logosmiley1 DAT 0x0000, 0xF014, 0xF015, 0xF016, 0xF017, 0x0000
+:logosmiley2 DAT 0xF033, 0xF034, 0xF035, 0xF036, 0xF037, 0xF038
+:logosmiley3 DAT 0x0000, 0xF039, 0xF03A, 0xF03B, 0xF03C, 0x0000
+:logosmiley2g DAT 0xF033, 0xF018, 0xF05C, 0xF07C, 0xF01C, 0xF038
+
+:blklogosmiley1 DAT 0xFF00, 0x0F14, 0x0F15, 0x0F16, 0x0F17, 0xFF00
+:blklogosmiley2 DAT 0x0F33, 0x0F18, 0x0F5C, 0x0F7C, 0x0F1C, 0x0F38
+:blklogosmiley3 DAT 0xFF00, 0x0F39, 0x0F3A, 0x0F3B, 0x0F3C, 0xFF00
+
+:spectrum DAT 0x0001, 0x0009, 0x0003, 0x000B, 0x0003, 0x0009
+
+:glasses1 DAT 0xF019, 0xF01A, 0xF01B, 0xF01C
+:glasses2 DAT 0xF01D, 0xF01E, 0xF01F, 0xF03D
+:glasses3 DAT 0xF03F, 0xF03F, 0xF03F, 0xF03F
+:glasses4 DAT 0x0000, 0xF05F, 0xF07D, 0xF07E
+:glasblank DAT 0x0000, 0x0000, 0x0000, 0x0000
+
+:minewhitetop DAT 0xF000, 0xF001, 0xF002, 0xF003, 0xF004, 0xF005, 0xF006, 0xF007, 0xF008, 0xF009, 0xF00A, 0xF00B, 0xF00C, 0xF00D, 0xF00E, 0xF00F, 0xF010, 0xF011, 0xF012
+
+:minewhitebtm DAT 0xF020, 0xF021, 0xF022, 0xF023, 0xF024, 0xF025, 0xF026 0xF027, 0xF028, 0xF029, 0xF02A, 0xF02B, 0xF02C, 0xF02D, 0xF02E, 0xF02F, 0xF030, 0xF031, 0xF032, 0xF032
+
+:sweeperwhitetop DAT 0xF040, 0xF041, 0xF042, 0xF043, 0xF044, 0xF045, 0xF046, 0xF047, 0xF048, 0xF049, 0xF04A, 0xF04B, 0xF04C, 0xF04D, 0xF04E, 0xF04F, 0xF050, 0xF051, 0xF052, 0xF053, 0xF054, 0xF055, 0xF056, 0xF057, 0xF058, 0xF059, 0xF05A, 0xF05B
+
+:sweeperwhitebtm DAT 0xF060, 0xF061, 0xF062, 0xF063, 0xF064, 0xF065, 0xF066 0xF067, 0xF068, 0xF069, 0xF06A, 0xF06B, 0xF06C, 0xF06D, 0xF06E, 0xF06F, 0xF070, 0xF071, 0xF072, 0xF073, 0xF074, 0xF075, 0xF076, 0xF077, 0xF078, 0xF079, 0xF07A, 0xF07B
+
+:mines DAT 10 ;change to your liking
+:minestext DAT 0x004E, 0x0045, 0x004E ;you should probably change this too
+
+:intro
+SET A, intro0
+SET B, 128
+SET C, 0x8180
+JSR storetiles
+SET I, 0
+SET A, 0x8000
+
+SET A, logosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, logosmiley2
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, logosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+
+SET I, 0
+
+:glasses_fall_loop ;deal with it, limited memory
+
+SET J, 0
+:slowdown_anim
+ADD J, 1
+IFN J, 1000
+ SET PC, slowdown_anim
+
+SET B, 3
+SET X, I
+MOD X, 3 ;3 animation frames
+SET C, 0x800E
+SET Z, I
+DIV Z, 3 ;y pos
+MUL Z, 32
+ADD C, Z
+
+IFE X, 0
+ SET A, glasses1
+IFE X, 1
+ SET A, glasses2
+IFE X, 2
+ SET A, glasses3
+
+JSR drawtiles
+
+SET A, glasblank
+SUB C, 32
+JSR drawtiles
+
+IFE X, 2
+ JSR glassesbottom
+
+ADD I, 1
+
+IFN I, 17
+ SET PC, glasses_fall_loop
+
+;draw white smiley
+SET A, logosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, logosmiley2g
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, logosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+;draw mine in white
+SET A, minewhitetop
+SET B, 18
+SET C, 0x8046
+JSR drawtiles
+SET A, minewhitebtm
+SET B, 18
+SET C, 0x8066
+JSR drawtiles
+;draw sweeper in white
+SET A, sweeperwhitetop
+SET B, 27
+SET C, 0x80E2
+JSR drawtiles
+SET A, sweeperwhitebtm
+SET B, 27
+SET C, 0x8102
+JSR drawtiles
+
+SET J, 0 ;wait for a bit
+:wait_logo
+ADD J, 1
+IFN J, 1000
+ SET PC, wait_logo
+
+SET J, 0 ;wait for a bit
+SET A, 0x8000
+:white_screen
+SET [A], 0xFF00
+ADD J, 1
+ADD A, 1
+IFN J, 384
+ SET PC, white_screen
+
+
+;draw black smiley
+SET A, blklogosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, blklogosmiley2
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, blklogosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+
+SET J, 0 ;cycle for a bit
+:cycle_logo
+ADD J, 1
+
+;draw mine in rainbow
+SET A, minewhitetop
+SET B, 18
+SET C, 0x8046
+JSR drawrainbow
+SET A, minewhitebtm
+SET B, 18
+SET C, 0x8066
+JSR drawrainbow
+;draw sweeper in rainbow
+SET A, sweeperwhitetop
+SET B, 27
+SET C, 0x80E2
+JSR drawrainbow
+SET A, sweeperwhitebtm
+SET B, 27
+SET C, 0x8102
+JSR drawrainbow
+
+IFN J, 66
+ SET PC, cycle_logo ;just animate a few times as a kind of wait
+
+SET PC, POP
+
+:drawrainbow
+SET PUSH, I
+SET PUSH, C
+SET I, 0
+SET X, J
+MOD X, 6 ;number of colours in spectrum
+
+:drawrainbow_loop
+SET [C], [A]
+SHL [C], 8
+SHR [C], 8
+ADD [C], 0x0F00
+SET Y, X
+ADD Y, spectrum ;select colour from spectrum
+SET Y, [Y]
+SHL Y, 12
+BOR [C], Y
+ADD C, 1
+ADD A, 1
+ADD I, 1
+ADD X, 1
+MOD X, 6
+IFG I, B
+ SET PC, drawrainbow_end
+SET PC, drawrainbow_loop
+
+:drawrainbow_end
+SET C, POP
+SET I, POP
+SET PC, POP
+
+:glassesbottom
+ADD C, 64
+SET A, glasses4
+JSR drawtiles
+SET PC, POP
+
+:clear_board
+SET I, 0
+SET J, board
+SET Y, flag
+
+:clearboard_loop
+SET [J], 0
+SET [Y], 0
+ADD J, 1
+ADD Y, 1
+ADD I, 1
+IFN I, 81
+ SET PC, clearboard_loop
+
+SET A, tiletypes_left ;reset mines too, shouldn't be visible
+ADD A, 10
+SET [A], 0x8701
+
+SET A, tiletypes_right
+ADD A, 10
+SET [A], 0x8702
+
+SET PC, POP
+
+:drawtext ; Y is colour
+SET PUSH, I
+SET I, 0
+SET PUSH, X
+
+:drawtext_loop
+SET X, [A]
+BOR X, Y
+SET [C], X
+ADD C, 1
+ADD A, 1
+ADD I, 1
+IFG I, B
+ SET PC, drawtext_end
+SET PC, drawtext_loop
+
+:drawtext_end
+SET X, POP
+SET I, POP
+SET PC, POP
+
+:gamestate DAT 0 ;0 = normal, 1 = win, 2 = lose
+
+:endgame
+
+IFE [gamestate], 1
+ SET A, youwintext
+IFE [gamestate], 2
+ SET A, youlosetext
+SET B, 6
+SET C, 0x816C
+IFE [gamestate], 1
+ SET Y, 0x9700
+IFE [gamestate], 2
+ SET Y, 0x4700
+JSR drawtext
+
+:endgame_loop
+
+SET C, 0x9000
+SET B, 0x0000
+:buffloop2
+ IFN [C], 0
+ SET B, [C]
+ SET [C], 0
+ ADD C, 1
+ IFN C, 0x9010
+ SET pc, buffloop2
+
+IFE B, 0x000a
+ SET PC, restart
+
+SET [C], 0
+SET C, 0x9000
+
+SET PC, endgame_loop
+
+;--------------------------------------------------------------------
+;this is the gameloop
+;it loops while the game is running
+; :)
+;--------------------------------------------------------------------
+
+:gameloop
+IFN [gamestate], 0 ;won or lost
+ SET PC, endgame
+ADD [timer], 1
+IFE [game_started], 0 ;timer is non functional
+ SET PC, skip_timer
+
+SET A, [timer]
+MOD A, [timer_second_value]
+IFN A, 0
+ SET PC, skip_timer
+
+ADD [gametimer], 1
+IFG [gametimer], 999
+ SET [gametimer], 999
+
+JSR draw_timer
+
+:skip_timer
+SET C, 0x9000
+SET B, 0x0000
+:buffloop
+ IFN [C], 0
+ SET B, [C]
+ SET [C], 0
+ ADD C, 1
+ IFN C, 0x9010
+ SET pc, buffloop
+
+SET A, 0
+IFE B, 0x0025
+ SET A, 2
+IFE B, 0x0026
+ SET A, 4
+IFE B, 0x0027
+ SET A, 1
+IFE B, 0x0028
+ SET A, 3
+IFE B, 0x000a
+ JSR action
+IFE B, 0x0066 ;f to flag
+ JSR flagspace
+
+IFN A, 0
+ JSR select_move
+
+SET [C], 0
+
+
+SET PC, gameloop
+
+:select_move
+IFE A, 1 ;right
+ ADD [select_x], 1
+IFE A, 2 ;left
+ SUB [select_x], 1
+IFE A, 3 ;down
+ ADD [select_y], 1
+IFE A, 4 ;up
+ SUB [select_y], 1
+IFG [select_x], 10
+ SET [select_x], 0
+IFG [select_y], 10
+ SET [select_y], 0
+IFG [select_x], 8
+ SET [select_x], 8
+IFG [select_y], 8
+ SET [select_y], 8
+
+JSR change_selection
+SET PC, POP
+
+:draw_timer
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+SET A, 0x813B ;timer's positon on the screen
+
+SET C, [gametimer]
+DIV C, 100 ;to get NXX (n being this character)
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+ADD A, 1
+
+SET C, [gametimer]
+DIV C, 10 ;to get XNX (n being this character)
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+ADD A, 1
+
+SET C, [gametimer]
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+
+SET C, POP
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:flagspace
+SET PUSH, A
+SET A, [select_x]
+SET B, [select_y]
+MUL B, 9
+ADD A, B
+ADD A, flag ;move to flag memory
+ADD [A], 1 ;change flag value
+MOD [A], 2 ;can only be 0 or 1
+
+JSR redraw_board
+
+SET A, POP
+SET PC, POP
+
+:action
+IFE [game_started], 0
+ JSR generate_board
+
+SET [game_started], 1
+
+SET PUSH, A
+SET PUSH, C
+
+SET A, [select_x]
+SET B, [select_y]
+SET X, A ;get normal position for mine seeking
+SET Y, B ;^^^
+MUL B, 9
+ADD A, B
+SET Z, A
+ADD Z, flag ;move to flag memory
+ADD A, board ;move to board memory
+
+IFE [Z], 1 ;space is flagged
+ SET PC, end_action ;yo don't do it bro
+
+IFE [A], 0
+ SET PC, mine_seek
+IFE [A], 10
+ JSR lose
+
+:end_action
+
+SET C, POP
+SET A, POP
+;SET J, POP ;j-pop is shit
+SET PC, POP
+
+:mine_seek
+SET Z, 1
+SET I, 0
+SET J, surrounding_work
+IFG [A], 0
+ ADD I, 1
+IFG 10, [A]
+ ADD I, 1
+IFG I, 1 ;within selected tile range
+ SET PC, POP ;do nothing because the tile's already selected
+
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+
+SET A, surprised_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, surprised_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+SET C, POP
+SET B, POP
+SET A, POP
+
+SET I, 0
+JSR check_surroundings ;check the surroundings of the selected tile
+IFE I, 0
+ SET PC, skip_surrounding_loop ;if it's secluded don't do anything (or it'll like crash or something)
+
+:mine_seek_loop ;oh god
+JSR move_surrounding_table
+SET J, surrounding_work
+SET I, 0
+
+:surrounding_process_loop
+IFE [J], 0
+ SET PC, skip_surrounding
+SET Y, J
+SUB Y, surrounding_work
+SET A, Y
+ADD A, board
+DIV Y, 9 ;remove x
+SET X, J
+SUB X, surrounding_work
+MOD X, 9
+JSR check_surroundings ;check the surroundings of the selected tile
+:skip_surrounding
+ADD J, 1
+IFN [J], 2 ;end byte 2
+ SET PC, surrounding_process_loop
+
+IFN I, 0 ;if nothing was added to surrounding exit the loops
+ SET PC, mine_seek_loop
+
+:skip_surrounding_loop
+
+JSR redraw_board
+
+SET A, norm_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, norm_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+JSR wincheck
+
+SET PC, end_action
+
+:wincheck
+SET A, board
+SET I, 0
+
+:wincheckloop
+IFE [A], 0
+ SET PC, POP
+ADD I, 1
+ADD A, 1
+IFN I, 81
+ SET PC, wincheckloop
+SET [gamestate], 1 ;win is gamestate 1
+SET PC, POP
+
+:move_surrounding_table
+SET A, surrounding_next
+SET B, surrounding_work
+
+:move_s_table_loop
+SET [B], [A] ;just put it into the read table
+SET [A], 0 ;simultaneously clearing the write table
+ADD A, 1
+ADD B, 1
+IFN [A], 2 ;if it hasn't reached the end byte (2, how imaginative) then loop
+ SET PC, move_s_table_loop
+SET PC, POP
+
+:redraw_board
+SET X, 0
+SET Y, 0
+SET I, 0
+SET A, board
+SET Z, flag
+SET PC, redraw_loop
+
+
+:tiletypes_left DAT 0x8701, 0x7F1E, 0x7F21, 0x7F23, 0x7F25, 0x7F27, 0x7F29, 0x7F2B, 0x7F2D, 0x7F2F, 0x8701, 0x3B31
+
+
+:tiletypes_right DAT 0x8702, 0x7F1F, 0x7F22, 0x7F24, 0x7F26, 0x7F28, 0x7F2A, 0x7F2C, 0x7F2E, 0x7F30, 0x8702, 0x3B32
+
+:redraw_loop
+SET B, X
+SET C, Y
+MUL B, 2
+ADD B, 7 ;change board pos to screen pos
+ADD C, 2
+MUL C, 32
+ADD B, C
+ADD B, 0x8000 ;move to video ram
+
+SET C, [A]
+
+IFN [Z], 1 ;flagged
+ SET PC, ingame_continue
+
+SET J, tiletypes_left
+ADD J, [A]
+
+IFE [J], 0x8701 ;if hidden and flagged then show the flag
+ SET C, 11
+
+:ingame_continue
+SET J, tiletypes_left
+ADD J, C
+SET [B], [J]
+
+ADD B, 1
+
+SET J, tiletypes_right
+ADD J, C
+SET [B], [J]
+
+ADD I, 1
+ADD X, 1
+ADD A, 1
+ADD Z, 1
+IFG X, 8
+ ADD Y, 1
+IFG X, 8
+ SET X, 0
+IFG 81, I
+ SET PC, redraw_loop ; loop until whole thing is done
+
+JSR redefine_selection ; the selected tile may have changed
+JSR change_selection
+
+ADD [moves], 1
+
+SET PC, POP
+
+:moves DAT 0
+
+:redefine_selection
+SET B, [select_x]
+SET C, [select_y]
+MUL B, 2
+ADD B, 7 ;change board pos to screen pos
+ADD C, 2
+MUL C, 32
+ADD B, C
+ADD B, 0x8000 ;move to video ram
+SET [old_select_tile1], [B]
+ADD B, 1
+SET [old_select_tile2], [B]
+SET PC, POP
+
+:check_surroundings ;i had to fix endless bugs with this
+SET Z, 1
+
+ADD X, 1 ;check right
+ADD A, 1
+ JSR checkblock
+SUB Y, 1 ;check up-right
+SUB A, 9
+ JSR checkblock
+SUB X, 1 ;check up
+SUB A, 1
+ JSR checkblock
+SUB X, 1 ;check up-left
+SUB A, 1
+ JSR checkblock
+ADD Y, 1 ;check left
+ADD A, 9
+ JSR checkblock
+ADD Y, 1 ;check down-left
+ADD A, 9
+ JSR checkblock
+ADD X, 1 ;check down
+ADD A, 1
+ JSR checkblock
+ADD X, 1 ;check down-right
+ADD A, 1
+ JSR checkblock
+SUB X, 1
+SUB Y, 1
+SUB A, 10 ;back to center
+SET [A], Z ;set tile type
+IFG 2, Z
+ JSR countsurroundings ;if this isn't a number then every surrounding block becomes visible
+SET PC, POP
+
+:countsurroundings
+ADD X, 1 ;check right
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+SUB Y, 1 ;check up-right
+SUB A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+SUB X, 1 ;check up
+SUB A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+SUB X, 1 ;check up-left
+SUB A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+ADD Y, 1 ;check left
+ADD A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+ADD Y, 1 ;check down-left
+ADD A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+ADD X, 1 ;check down
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+ADD X, 1 ;check down-right
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+
+SET PC, POP
+
+:add_to_surroundings
+ADD I, 1
+IFG X, 8 ;illegal block
+ SET PC, POP
+IFG Y, 8 ;illegal block
+ SET PC, POP
+SET B, A
+SUB B, board
+ADD B, surrounding_next ;change from board id into surroundings id
+SET [B], 1 ;1 means active
+SET PC, POP
+
+:checkblock
+IFG X, 8 ;illegal block
+ SET PC, POP
+IFG Y, 8 ;illegal block
+ SET PC, POP
+IFE [A], 10
+ ADD Z, 1 ;increment block number
+SET PC, POP
+
+:lose
+SET A, wtf_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, wtf_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+SET A, tiletypes_left
+ADD A, 10
+SET [A], 0x4C33
+
+SET A, tiletypes_right
+ADD A, 10
+SET [A], 0x4C34
+
+JSR redraw_board
+
+SET [gamestate], 2 ;2 is lose
+
+SET PC, POP ;????
+
+
+:generate_board
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+SET PUSH, X
+SET PUSH, I ;iterator
+SET I, 0
+
+:g_board_loop
+ADD [timer], 1
+SET A, [timer]
+MUL A, A ;entropy
+MOD A, 9 ;a is x of random mine placement
+SET B, [timer]
+ADD B, 317 ;b is y of mine placement, needs to be very different from A
+MUL B, B
+MOD B, 9
+
+SET C, A ;check if in proximity to cursor, if yes then position again. x
+SUB C, [select_x]
+ADD C, 10 ;work around underflow
+SET X, 0 ;used for checking and condition
+IFG C, 8
+ ADD X, 1
+IFG 12, C
+ ADD X, 1
+
+SET C, B ;do it for the y placement too
+SUB C, [select_y]
+ADD C, 10 ;work around underflow
+IFG C, 8
+ ADD X, 1
+IFG 12, C
+ ADD X, 1
+
+
+IFE X, 4
+ SET PC, g_board_loop
+
+MUL B, 9
+ADD A, B
+SET C, board
+ADD C, A
+IFE [C], 10 ;defined at top
+ SET PC, g_board_loop ;already a mine there
+SET [C], 10
+
+ADD I, 1
+IFG [mines], I
+ SET PC, g_board_loop ;not enough mines
+
+SET [timer], 0
+
+SET I, POP
+SET X, POP
+SET C, POP
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:old_select_operation
+SET [A], [old_select_tile1]
+ADD A, 1 ;and the other side
+SET [A], [old_select_tile2]
+SET PC, POP
+
+:change_selection
+SET PUSH, A ; x value
+SET PUSH, B ; y value
+SET A, [old_select_x]
+SET B, [old_select_y]
+JSR get_selection
+
+IFN [old_select_x], 10 ;10 is the number which tells it not to do anything
+ JSR old_select_operation
+
+SET A, [select_x]
+SET B, [select_y]
+JSR get_selection
+
+SET [old_select_tile1], [A]
+SHL [A], 4 ;remove highest 2 bits
+SHR [A], 4
+BOR [A], 0x2A00
+ADD A, 1 ;and the other side
+SET [old_select_tile2], [A]
+SHL [A], 4 ;remove highest 2 bits
+SHR [A], 4
+BOR [A], 0x2A00
+
+SET [old_select_x], [select_x]
+SET [old_select_y], [select_y]
+
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:get_selection
+ADD B, 2
+MUL A, 2
+ADD A, 7 ;change x and y into screen position instead of game position
+MUL B, 32 ;multiply y by screen width to get memory offset
+ADD A, B ;the final offset moves into A
+ADD A, 0x8000 ;now it's the memory address, wasn't that fun
+SET PC, POP
+
+:old_select_x DAT 10 ;doesn't exist
+:old_select_y DAT 10 ;doesn't exist
+:old_select_tile1 DAT 10 ;ok that does exist but you get the point
+:old_select_tile2 DAT 10
+:select_x DAT 4
+:select_y DAT 4
+
+:timer DAT 0
+:game_started DAT 0
+
+:flag DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+
+:board DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;10 for mines, 0 for undiscovered, 1-9 for selected, 11 for flagged.
+
+:surrounding_work DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;working table, reads from
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
+
+:surrounding_next DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;next table, writes to
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 ;board of surrounding tiles because my last idea overflowed all the way into the graphics :@
+
+:end
+SET PC, end
+
+:drawtiles
+SET PUSH, I
+SET PUSH, C
+SET I, 0
+
+:drawtiles_loop
+SET [C], [A]
+ADD C, 1
+ADD A, 1
+ADD I, 1
+IFG I, B
+ SET PC, drawtiles_end
+SET PC, drawtiles_loop
+
+:drawtiles_end
+SET C, POP
+SET I, POP
+SET PC, POP
+
+;tilemap data for the main map
+
+:norm_top DAT 0x6F03, 0x6F04, 0x6F05, 0x6F06
+:norm_btm DAT 0x6F07, 0x6F08, 0x6F09, 0x6F0A
+
+:surprised_top DAT 0x6F35, 0x6F36, 0x6F37, 0x6F38
+:surprised_btm DAT 0x6F39, 0x6F3A, 0x6F3B, 0x6F3C
+
+:wtf_top DAT 0x6F3D, 0x6F3E, 0x6F3F, 0x6F40
+:wtf_btm DAT 0x6F41, 0x6F42, 0x6F43, 0x6F44
+
+:boardtiles_l1 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x6F03, 0x6F04, 0x6F05, 0x6F06, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l2 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70B, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x6F07, 0x6F08, 0x6F09, 0x6F0A, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x70C, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l3 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l4 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l5 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l6 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l7 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l8 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l9 DAT 0x8720, 0x8713, 0x8714, 0x8715, 0x8716, 0x8717, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8718, 0x8719, 0x871A, 0x871B, 0x871C, 0x8720
+
+:boardtiles_20 DAT 0x8720, 0x871D, 0x8F20, 0x8F20, 0x8F20, 0x8710, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x871D, 0x8F20, 0x8F20, 0x8F20, 0x8710, 0x8720
+
+:boardtiles_21 DAT 0x8720, 0x870D, 0x8712, 0x8712, 0x8712, 0x870E, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x870D, 0x8712, 0x8712, 0x8712, 0x870E, 0x8720
+
+:boardtiles_22 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70D, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x70E, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_22 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70D, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x70E, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+
+; Tiles.
+
+:tile0 DAT 0xff83, 0x45a9
+:tile1 DAT 0x51b9, 0x7dff
+:tile2 DAT 0xff01, 0x1c1
+:tile3 DAT 0x7151, 0xc9c9
+:tile4 DAT 0x49c9, 0xd171
+:tile5 DAT 0xc101, 0x1ff
+:tile6 DAT 0xff80, 0x8083
+:tile7 DAT 0x8c89, 0x9292
+:tile8 DAT 0x9290, 0x888c
+:tile9 DAT 0x8380, 0x80ff
+:tile10 DAT 0x0, 0x80
+:tile11 DAT 0x8000, 0x0
+:tile12 DAT 0x0, 0x1
+:tile13 DAT 0x100, 0x0
+:tile14 DAT 0x0, 0x55ff
+:tile15 DAT 0xff00, 0x0
+:tile16 DAT 0xc080, 0xc080
+:tile17 DAT 0x101, 0x101
+:tile18 DAT 0x3c, 0x888
+:tile19 DAT 0xbc80, 0xbc80
+:tile20 DAT 0xbc88, 0x90bc
+:tile21 DAT 0x80bc, 0xaca4
+:tile22 DAT 0x8028, 0x2414
+:tile23 DAT 0x0, 0x84
+:tile24 DAT 0xbc84, 0x80bc
+:tile25 DAT 0x80bc, 0x8888
+:tile26 DAT 0xbc80, 0xbcac
+:tile27 DAT 0xa400, 0x0
+:tile28 DAT 0x0, 0xff
+:tile29 DAT 0xff01, 0x101
+:tile30 DAT 0x101, 0x101
+:tile31 DAT 0x0, 0x0
+:tile32 DAT 0xff01, 0x149
+:tile33 DAT 0x7d41, 0x101
+:tile34 DAT 0xff01, 0x149
+:tile35 DAT 0x6559, 0x101
+:tile36 DAT 0xff01, 0x145
+:tile37 DAT 0x5539, 0x101
+:tile38 DAT 0xff01, 0x11d
+:tile39 DAT 0x117d, 0x101
+:tile40 DAT 0xff01, 0x15d
+:tile41 DAT 0x5525, 0x101
+:tile42 DAT 0xff01, 0x139
+:tile43 DAT 0x5525, 0x101
+:tile44 DAT 0xff01, 0x145
+:tile45 DAT 0x350d, 0x101
+:tile46 DAT 0xff01, 0x129
+:tile47 DAT 0x5529, 0x101
+:tile48 DAT 0xff83, 0x10d
+:tile49 DAT 0xd7d, 0x183
+:tile50 DAT 0xff01, 0x5539
+:tile51 DAT 0x6d39, 0x5501
+:tile52 DAT 0xff01, 0x1e1
+:tile53 DAT 0x3131, 0xa969
+:tile54 DAT 0x69a9, 0x7171
+:tile55 DAT 0xa101, 0x1ff
+:tile56 DAT 0xff80, 0x8083
+:tile57 DAT 0x8c88, 0x9096
+:tile58 DAT 0x9690, 0x888c
+:tile59 DAT 0x8380, 0x80ff
+:tile60 DAT 0xff01, 0x1c1
+:tile61 DAT 0x3151, 0xc949
+:tile62 DAT 0x2999, 0x1131
+:tile63 DAT 0xc101, 0x1ff
+:tile64 DAT 0xff80, 0x8083
+:tile65 DAT 0xcc88, 0xd0de
+:tile66 DAT 0x9ed0, 0xc8cc
+:tile67 DAT 0x8380, 0x80ff
+:tile68 DAT 0x48, 0x7c40
+:tile69 DAT 0x48, 0x6458
+:tile70 DAT 0x44, 0x5438
+:tile71 DAT 0x1c, 0x107c
+:tile72 DAT 0x5c, 0x5424
+:tile73 DAT 0x38, 0x5424
+:tile74 DAT 0x44, 0x340c
+:tile75 DAT 0x28, 0x5428
+:tile76 DAT 0x48, 0x5438
+:tile77 DAT 0x38, 0x4438
+:tile78 DAT 0x18, 0xe018
+:tile79 DAT 0x60, 0x9060
+:tile80 DAT 0x70, 0x80f0
+:tile81 DAT 0x0, 0xf8
+:tile82 DAT 0x8040, 0x80f8
+:tile83 DAT 0xe8, 0xf0
+:tile84 DAT 0x20e0, 0x0
+:tile85 DAT 0xf8, 0x8080
+:tile86 DAT 0x60, 0x9060
+:tile87 DAT 0xb0, 0x90d0
+:tile88 DAT 0xf0, 0x90b0
+
+;1kb worth of tiles used for the intro sequence. worth it!
+
+
+:intro0 DAT 0x0, 0x101
+:intro1 DAT 0xc3eb, 0xebeb
+:intro2 DAT 0xebea, 0xe8e0
+:intro3 DAT 0xc000, 0x80e0
+:intro4 DAT 0xe0e8, 0xeaeb
+:intro5 DAT 0xebeb, 0xb01
+:intro6 DAT 0x101, 0x0
+:intro7 DAT 0x1, 0x1e1
+:intro8 DAT 0xebeb, 0xebeb
+:intro9 DAT 0xeb0b, 0x101
+:intro10 DAT 0x100, 0x101
+:intro11 DAT 0xc1eb, 0xebeb
+:intro12 DAT 0xebea, 0xe8eb
+:intro13 DAT 0xe3eb, 0x2b03
+:intro14 DAT 0x101, 0x0
+:intro15 DAT 0x101, 0xc1eb
+:intro16 DAT 0xebeb, 0xebeb
+:intro17 DAT 0xeb43, 0xc1c1
+:intro18 DAT 0x303, 0xb00
+:intro19 DAT 0x0, 0x0
+:intro20 DAT 0xf0f0, 0x3030
+:intro21 DAT 0xc0c, 0xc0c
+:intro22 DAT 0xc0c, 0xc0c
+:intro23 DAT 0x3030, 0xf0f0
+:intro24 DAT 0x303, 0x3333
+:intro25 DAT 0x303, 0x303
+:intro26 DAT 0xf0f, 0xf0f
+:intro27 DAT 0x303, 0xf0f
+:intro28 DAT 0xf0f, 0x303
+:intro29 DAT 0x1818, 0x1818
+:intro30 DAT 0x7878, 0x7878
+:intro31 DAT 0x1818, 0x7878
+:intro32 DAT 0x4040, 0x6078
+:intro33 DAT 0x7f61, 0x434f
+:intro34 DAT 0x1f7f, 0x3f1f
+:intro35 DAT 0x4f47, 0x417f
+:intro36 DAT 0x7f7f, 0x7f7f
+:intro37 DAT 0x7f6f, 0x4040
+:intro38 DAT 0x0, 0x40
+:intro39 DAT 0x4040, 0x787f
+:intro40 DAT 0x7f7f, 0x7f7f
+:intro41 DAT 0x4740, 0x4040
+:intro42 DAT 0x40, 0x6070
+:intro43 DAT 0x7f71, 0x6347
+:intro44 DAT 0xf1f, 0x3f7f
+:intro45 DAT 0x7f0f, 0x0
+:intro46 DAT 0x0, 0x4040
+:intro47 DAT 0x4060, 0x7f7f
+:intro48 DAT 0x7f7f, 0x7f7f
+:intro49 DAT 0x4340, 0x6073
+:intro50 DAT 0x703c, 0x600
+:intro51 DAT 0x0, 0xffff
+:intro52 DAT 0x0, 0x3030
+:intro53 DAT 0xc3c3, 0xc0c0
+:intro54 DAT 0xc0c0, 0x303
+:intro55 DAT 0x0, 0x0
+:intro56 DAT 0xffff, 0x0
+:intro57 DAT 0xf0f, 0xc0c
+:intro58 DAT 0x3030, 0x3030
+:intro59 DAT 0x3030, 0x3030
+:intro60 DAT 0xc0c, 0xf0f
+:intro61 DAT 0x7878, 0x1818
+:intro62 DAT 0xc0c0, 0xc0c0
+:intro63 DAT 0xc0c0, 0xc0c0
+:intro64 DAT 0xc0d0, 0xd4d4
+:intro65 DAT 0xd2d2, 0xc2c6
+:intro66 DAT 0xc6d6, 0x601
+:intro67 DAT 0x0, 0x216
+:intro68 DAT 0xd6d6, 0xd6d6
+:intro69 DAT 0xd6d2, 0x12d0
+:intro70 DAT 0xd0d0, 0xd4d4
+:intro71 DAT 0xd486, 0x284
+:intro72 DAT 0x4416, 0x602
+:intro73 DAT 0x2, 0x2d6
+:intro74 DAT 0xd6d6, 0xd6d6
+:intro75 DAT 0xd686, 0x8282
+:intro76 DAT 0x616, 0x0
+:intro77 DAT 0x2, 0x2d6
+:intro78 DAT 0xd6d6, 0xd6d6
+:intro79 DAT 0xd686, 0x8282
+:intro80 DAT 0x616, 0x0
+:intro81 DAT 0x2, 0x2d6
+:intro82 DAT 0xd6d6, 0xd6d6
+:intro83 DAT 0xd602, 0x2d6
+:intro84 DAT 0xd400, 0x2
+:intro85 DAT 0x2d6, 0xd6d6
+:intro86 DAT 0xd6d6, 0xd686
+:intro87 DAT 0x8282, 0x616
+:intro88 DAT 0x0, 0x0
+:intro89 DAT 0x202, 0xd6d6
+:intro90 DAT 0xd6d6, 0xd6d6
+:intro91 DAT 0x606, 0x8454
+:intro92 DAT 0xcfcf, 0xcfcf
+:intro93 DAT 0xc0c0, 0xc0c0
+:intro94 DAT 0xc0c0, 0xc0c0
+:intro95 DAT 0x303, 0x303
+:intro96 DAT 0xfb67, 0xcf8f
+:intro97 DAT 0x9f9f, 0x9f7f
+:intro98 DAT 0x7f1f, 0x700
+:intro99 DAT 0x0, 0x0
+:intro100 DAT 0xf7f, 0xffff
+:intro101 DAT 0x3f1f, 0xe03
+:intro102 DAT 0xf3f, 0xff7f
+:intro103 DAT 0x3f0f, 0x601
+:intro104 DAT 0x0, 0x80
+:intro105 DAT 0x8080, 0xf0ff
+:intro106 DAT 0xffff, 0xffff
+:intro107 DAT 0x87c0, 0xc3e7
+:intro108 DAT 0xf00c, 0x80
+:intro109 DAT 0x8080, 0xf0ff
+:intro110 DAT 0xffff, 0xffff
+:intro111 DAT 0x87c0, 0xc3e7
+:intro112 DAT 0xf00c, 0x80
+:intro113 DAT 0x80c0, 0xfeff
+:intro114 DAT 0xffff, 0xffff
+:intro115 DAT 0x8282, 0x301
+:intro116 DAT 0x80, 0x8080
+:intro117 DAT 0xf0ff, 0xffff
+:intro118 DAT 0xffff, 0x87c0
+:intro119 DAT 0xc3e7, 0xf00c
+:intro120 DAT 0x0, 0x8080
+:intro121 DAT 0xc0fe, 0xffff
+:intro122 DAT 0xffff, 0xbf81
+:intro123 DAT 0x83ff, 0xfffe
+:intro124 DAT 0xc3c3, 0xf0f
+:intro125 DAT 0x0, 0x303
+:intro126 DAT 0x303, 0x0
+:intro127 DAT 0x0, 0x0
+
+
+
+:storetiles ;A = Data to Read, B = Length, C = Destination, I = Iterator
+SET PUSH, I
+MUL B, 2 ;2 words to a character
+SET I, 0
+
+:storetiles_loop
+SET [C], [A]
+ADD A, 1
+ADD I, 1
+ADD C, 1
+IFE I, B
+ SET PC, storetiles_end
+SET PC, storetiles_loop
+
+:storetiles_end
+SET I, POP
+SET PC, POP
diff --git a/examples/minesweeper.s.orig b/examples/minesweeper.s.orig
new file mode 100644
index 0000000..78d0d8a
--- /dev/null
+++ b/examples/minesweeper.s.orig
@@ -0,0 +1,1272 @@
+; MINESWEEPER by RHY3756547
+;
+; My first DCPU-16 project
+; Feel free to fork and port for operating systems.
+; Heavily commented. :>
+;
+; Twitter: #RHY3756547
+
+; ARROW KEYS to move, ENTER to select.
+; F to toggle flag on location.
+
+; Upon winning or losing, press enter to start again.
+
+JSR intro
+
+SET A, tile0
+SET B, 89
+SET C, 0x8182
+JSR storetiles
+:restart
+SET [gamestate], 0
+SET [gametimer], 0
+SET [game_started], 0
+SET [old_select_x], 10
+SET [old_select_y], 10
+JSR clear_board
+SET A, boardtiles_l1
+SET B, 384
+SET C, 0x8000
+JSR drawtiles
+
+SET A, minestext
+SET B, 2
+SET C, 0x8122
+SET Y, 0x8F00
+JSR drawtext
+JSR draw_timer
+
+JSR change_selection
+SET PC, gameloop
+
+:gametimer DAT 0
+:timer_second_value DAT 575
+:youwintext DAT 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055
+:youlosetext DAT 0x004F, 0x0050, 0x0051, 0x0056, 0x0057, 0x0058, 0x0059
+:logosmiley1 DAT 0x0000, 0xF014, 0xF015, 0xF016, 0xF017, 0x0000
+:logosmiley2 DAT 0xF033, 0xF034, 0xF035, 0xF036, 0xF037, 0xF038
+:logosmiley3 DAT 0x0000, 0xF039, 0xF03A, 0xF03B, 0xF03C, 0x0000
+:logosmiley2g DAT 0xF033, 0xF018, 0xF05C, 0xF07C, 0xF01C, 0xF038
+
+:blklogosmiley1 DAT 0xFF00, 0x0F14, 0x0F15, 0x0F16, 0x0F17, 0xFF00
+:blklogosmiley2 DAT 0x0F33, 0x0F18, 0x0F5C, 0x0F7C, 0x0F1C, 0x0F38
+:blklogosmiley3 DAT 0xFF00, 0x0F39, 0x0F3A, 0x0F3B, 0x0F3C, 0xFF00
+
+:spectrum DAT 0x0001, 0x0009, 0x0003, 0x000B, 0x0003, 0x0009
+
+:glasses1 DAT 0xF019, 0xF01A, 0xF01B, 0xF01C
+:glasses2 DAT 0xF01D, 0xF01E, 0xF01F, 0xF03D
+:glasses3 DAT 0xF03F, 0xF03F, 0xF03F, 0xF03F
+:glasses4 DAT 0x0000, 0xF05F, 0xF07D, 0xF07E
+:glasblank DAT 0x0000, 0x0000, 0x0000, 0x0000
+
+:minewhitetop DAT 0xF000, 0xF001, 0xF002, 0xF003, 0xF004, 0xF005, 0xF006, 0xF007, 0xF008, 0xF009, 0xF00A, 0xF00B, 0xF00C, 0xF00D, 0xF00E, 0xF00F, 0xF010, 0xF011, 0xF012
+
+:minewhitebtm DAT 0xF020, 0xF021, 0xF022, 0xF023, 0xF024, 0xF025, 0xF026 0xF027, 0xF028, 0xF029, 0xF02A, 0xF02B, 0xF02C, 0xF02D, 0xF02E, 0xF02F, 0xF030, 0xF031, 0xF032, 0xF032
+
+:sweeperwhitetop DAT 0xF040, 0xF041, 0xF042, 0xF043, 0xF044, 0xF045, 0xF046, 0xF047, 0xF048, 0xF049, 0xF04A, 0xF04B, 0xF04C, 0xF04D, 0xF04E, 0xF04F, 0xF050, 0xF051, 0xF052, 0xF053, 0xF054, 0xF055, 0xF056, 0xF057, 0xF058, 0xF059, 0xF05A, 0xF05B
+
+:sweeperwhitebtm DAT 0xF060, 0xF061, 0xF062, 0xF063, 0xF064, 0xF065, 0xF066 0xF067, 0xF068, 0xF069, 0xF06A, 0xF06B, 0xF06C, 0xF06D, 0xF06E, 0xF06F, 0xF070, 0xF071, 0xF072, 0xF073, 0xF074, 0xF075, 0xF076, 0xF077, 0xF078, 0xF079, 0xF07A, 0xF07B
+
+:mines DAT 10 ;change to your liking
+:minestext DAT 0x004E, 0x0045, 0x004E ;you should probably change this too
+
+:intro
+SET A, intro0
+SET B, 128
+SET C, 0x8180
+JSR storetiles
+SET I, 0
+SET A, 0x8000
+
+SET A, logosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, logosmiley2
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, logosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+
+SET I, 0
+
+:glasses_fall_loop ;deal with it, limited memory
+
+SET J, 0
+:slowdown_anim
+ADD J, 1
+IFN J, 1000
+ SET PC, slowdown_anim
+
+SET B, 3
+SET X, I
+MOD X, 3 ;3 animation frames
+SET C, 0x800E
+SET Z, I
+DIV Z, 3 ;y pos
+MUL Z, 32
+ADD C, Z
+
+IFE X, 0
+ SET A, glasses1
+IFE X, 1
+ SET A, glasses2
+IFE X, 2
+ SET A, glasses3
+
+JSR drawtiles
+
+SET A, glasblank
+SUB C, 32
+JSR drawtiles
+
+IFE X, 2
+ JSR glassesbottom
+
+ADD I, 1
+
+IFN I, 17
+ SET PC, glasses_fall_loop
+
+;draw white smiley
+SET A, logosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, logosmiley2g
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, logosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+;draw mine in white
+SET A, minewhitetop
+SET B, 18
+SET C, 0x8046
+JSR drawtiles
+SET A, minewhitebtm
+SET B, 18
+SET C, 0x8066
+JSR drawtiles
+;draw sweeper in white
+SET A, sweeperwhitetop
+SET B, 27
+SET C, 0x80E2
+JSR drawtiles
+SET A, sweeperwhitebtm
+SET B, 27
+SET C, 0x8102
+JSR drawtiles
+
+SET J, 0 ;wait for a bit
+:wait_logo
+ADD J, 1
+IFN J, 1000
+ SET PC, wait_logo
+
+SET J, 0 ;wait for a bit
+SET A, 0x8000
+:white_screen
+SET [A], 0xFF00
+ADD J, 1
+ADD A, 1
+IFN J, 384
+ SET PC, white_screen
+
+
+;draw black smiley
+SET A, blklogosmiley1
+SET B, 5
+SET C, 0x808D
+JSR drawtiles
+SET A, blklogosmiley2
+SET B, 5
+SET C, 0x80AD
+JSR drawtiles
+SET A, blklogosmiley3
+SET B, 5
+SET C, 0x80CD
+JSR drawtiles
+
+SET J, 0 ;cycle for a bit
+:cycle_logo
+ADD J, 1
+
+;draw mine in rainbow
+SET A, minewhitetop
+SET B, 18
+SET C, 0x8046
+JSR drawrainbow
+SET A, minewhitebtm
+SET B, 18
+SET C, 0x8066
+JSR drawrainbow
+;draw sweeper in rainbow
+SET A, sweeperwhitetop
+SET B, 27
+SET C, 0x80E2
+JSR drawrainbow
+SET A, sweeperwhitebtm
+SET B, 27
+SET C, 0x8102
+JSR drawrainbow
+
+IFN J, 66
+ SET PC, cycle_logo ;just animate a few times as a kind of wait
+
+SET PC, POP
+
+:drawrainbow
+SET PUSH, I
+SET PUSH, C
+SET I, 0
+SET X, J
+MOD X, 6 ;number of colours in spectrum
+
+:drawrainbow_loop
+SET [C], [A]
+SHL [C], 8
+SHR [C], 8
+ADD [C], 0x0F00
+SET Y, X
+ADD Y, spectrum ;select colour from spectrum
+SET Y, [Y]
+SHL Y, 12
+BOR [C], Y
+ADD C, 1
+ADD A, 1
+ADD I, 1
+ADD X, 1
+MOD X, 6
+IFG I, B
+ SET PC, drawrainbow_end
+SET PC, drawrainbow_loop
+
+:drawrainbow_end
+SET C, POP
+SET I, POP
+SET PC, POP
+
+:glassesbottom
+ADD C, 64
+SET A, glasses4
+JSR drawtiles
+SET PC, POP
+
+:clear_board
+SET I, 0
+SET J, board
+SET Y, flag
+
+:clearboard_loop
+SET [J], 0
+SET [Y], 0
+ADD J, 1
+ADD Y, 1
+ADD I, 1
+IFN I, 81
+ SET PC, clearboard_loop
+
+SET A, tiletypes_left ;reset mines too, shouldn't be visible
+ADD A, 10
+SET [A], 0x8701
+
+SET A, tiletypes_right
+ADD A, 10
+SET [A], 0x8702
+
+SET PC, POP
+
+:drawtext ; Y is colour
+SET PUSH, I
+SET I, 0
+SET PUSH, X
+
+:drawtext_loop
+SET X, [A]
+BOR X, Y
+SET [C], X
+ADD C, 1
+ADD A, 1
+ADD I, 1
+IFG I, B
+ SET PC, drawtext_end
+SET PC, drawtext_loop
+
+:drawtext_end
+SET X, POP
+SET I, POP
+SET PC, POP
+
+:gamestate DAT 0 ;0 = normal, 1 = win, 2 = lose
+
+:endgame
+
+IFE [gamestate], 1
+ SET A, youwintext
+IFE [gamestate], 2
+ SET A, youlosetext
+SET B, 6
+SET C, 0x816C
+IFE [gamestate], 1
+ SET Y, 0x9700
+IFE [gamestate], 2
+ SET Y, 0x4700
+JSR drawtext
+
+:endgame_loop
+
+SET C, 0x9000
+SET B, 0x0000
+:buffloop2
+ IFN [C], 0
+ SET B, [C]
+ SET [C], 0
+ ADD C, 1
+ IFN C, 0x9010
+ SET pc, buffloop2
+
+IFE B, 0x000a
+ SET PC, restart
+
+SET [C], 0
+SET C, 0x9000
+
+SET PC, endgame_loop
+
+;--------------------------------------------------------------------
+;this is the gameloop
+;it loops while the game is running
+; :)
+;--------------------------------------------------------------------
+
+:gameloop
+IFN [gamestate], 0 ;won or lost
+ SET PC, endgame
+ADD [timer], 1
+IFE [game_started], 0 ;timer is non functional
+ SET PC, skip_timer
+
+SET A, [timer]
+MOD A, [timer_second_value]
+IFN A, 0
+ SET PC, skip_timer
+
+ADD [gametimer], 1
+IFG [gametimer], 999
+ SET [gametimer], 999
+
+JSR draw_timer
+
+:skip_timer
+SET C, 0x9000
+SET B, 0x0000
+:buffloop
+ IFN [C], 0
+ SET B, [C]
+ SET [C], 0
+ ADD C, 1
+ IFN C, 0x9010
+ SET pc, buffloop
+
+SET A, 0
+IFE B, 0x0025
+ SET A, 2
+IFE B, 0x0026
+ SET A, 4
+IFE B, 0x0027
+ SET A, 1
+IFE B, 0x0028
+ SET A, 3
+IFE B, 0x000a
+ JSR action
+IFE B, 0x0066 ;f to flag
+ JSR flagspace
+
+IFN A, 0
+ JSR select_move
+
+SET [C], 0
+
+
+SET PC, gameloop
+
+:select_move
+IFE A, 1 ;right
+ ADD [select_x], 1
+IFE A, 2 ;left
+ SUB [select_x], 1
+IFE A, 3 ;down
+ ADD [select_y], 1
+IFE A, 4 ;up
+ SUB [select_y], 1
+IFG [select_x], 10
+ SET [select_x], 0
+IFG [select_y], 10
+ SET [select_y], 0
+IFG [select_x], 8
+ SET [select_x], 8
+IFG [select_y], 8
+ SET [select_y], 8
+
+JSR change_selection
+SET PC, POP
+
+:draw_timer
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+SET A, 0x813B ;timer's positon on the screen
+
+SET C, [gametimer]
+DIV C, 100 ;to get NXX (n being this character)
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+ADD A, 1
+
+SET C, [gametimer]
+DIV C, 10 ;to get XNX (n being this character)
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+ADD A, 1
+
+SET C, [gametimer]
+MOD C, 10 ;get lowest int
+SET B, 0x8F44
+ADD B, C ;get tile pos
+IFE C, 0
+ SET B, 0x8F4E ;0
+
+SET [A], B
+
+SET C, POP
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:flagspace
+SET PUSH, A
+SET A, [select_x]
+SET B, [select_y]
+MUL B, 9
+ADD A, B
+ADD A, flag ;move to flag memory
+ADD [A], 1 ;change flag value
+MOD [A], 2 ;can only be 0 or 1
+
+JSR redraw_board
+
+SET A, POP
+SET PC, POP
+
+:action
+IFE [game_started], 0
+ JSR, generate_board
+
+SET [game_started], 1
+
+SET PUSH, A
+SET PUSH, C
+
+SET A, [select_x]
+SET B, [select_y]
+SET X, A ;get normal position for mine seeking
+SET Y, B ;^^^
+MUL B, 9
+ADD A, B
+SET Z, A
+ADD Z, flag ;move to flag memory
+ADD A, board ;move to board memory
+
+IFE [Z], 1 ;space is flagged
+ SET PC, end_action ;yo don't do it bro
+
+IFE [A], 0
+ SET PC, mine_seek
+IFE [A], 10
+ JSR lose
+
+:end_action
+
+SET C, POP
+SET A, POP
+;SET J, POP ;j-pop is shit
+SET PC, POP
+
+:mine_seek
+SET Z, 1
+SET I, 0
+SET J, surrounding_work
+IFG [A], 0
+ ADD I, 1
+IFG 10, [A]
+ ADD I, 1
+IFG I, 1 ;within selected tile range
+ SET PC, POP ;do nothing because the tile's already selected
+
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+
+SET A, surprised_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, surprised_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+SET C, POP
+SET B, POP
+SET A, POP
+
+SET I, 0
+JSR check_surroundings ;check the surroundings of the selected tile
+IFE I, 0
+ SET PC, skip_surrounding_loop ;if it's secluded don't do anything (or it'll like crash or something)
+
+:mine_seek_loop ;oh god
+JSR move_surrounding_table
+SET J, surrounding_work
+SET I, 0
+
+:surrounding_process_loop
+IFE [J], 0
+ SET PC, skip_surrounding
+SET Y, J
+SUB Y, surrounding_work
+SET A, Y
+ADD A, board
+DIV Y, 9 ;remove x
+SET X, J
+SUB X, surrounding_work
+MOD X, 9
+JSR check_surroundings ;check the surroundings of the selected tile
+:skip_surrounding
+ADD J, 1
+IFN [J], 2 ;end byte 2
+ SET PC, surrounding_process_loop
+
+IFN I, 0 ;if nothing was added to surrounding exit the loops
+ SET PC mine_seek_loop
+
+:skip_surrounding_loop
+
+JSR redraw_board
+
+SET A, norm_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, norm_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+JSR wincheck
+
+SET PC, end_action
+
+:wincheck
+SET A, board
+SET I, 0
+
+:wincheckloop
+IFE [A], 0
+ SET PC, POP
+ADD I, 1
+ADD A, 1
+IFN I, 81
+ SET PC, wincheckloop
+SET [gamestate], 1 ;win is gamestate 1
+SET PC, POP
+
+:move_surrounding_table
+SET A, surrounding_next
+SET B, surrounding_work
+
+:move_s_table_loop
+SET [B], [A] ;just put it into the read table
+SET [A], 0 ;simultaneously clearing the write table
+ADD A, 1
+ADD B, 1
+IFN [A], 2 ;if it hasn't reached the end byte (2, how imaginative) then loop
+ SET PC, move_s_table_loop
+SET PC, POP
+
+:redraw_board
+SET X, 0
+SET Y, 0
+SET I, 0
+SET A, board
+SET Z, flag
+SET PC, redraw_loop
+
+
+:tiletypes_left DAT 0x8701, 0x7F1E, 0x7F21, 0x7F23, 0x7F25, 0x7F27, 0x7F29, 0x7F2B, 0x7F2D, 0x7F2F, 0x8701, 0x3B31
+
+
+:tiletypes_right DAT 0x8702, 0x7F1F, 0x7F22, 0x7F24, 0x7F26, 0x7F28, 0x7F2A, 0x7F2C, 0x7F2E, 0x7F30, 0x8702, 0x3B32
+
+:redraw_loop
+SET B, X
+SET C, Y
+MUL B, 2
+ADD B, 7 ;change board pos to screen pos
+ADD C, 2
+MUL C, 32
+ADD B, C
+ADD B, 0x8000 ;move to video ram
+
+SET C, [A]
+
+IFN [Z], 1 ;flagged
+ SET PC, ingame_continue
+
+SET J, tiletypes_left
+ADD J, [A]
+
+IFE [J], 0x8701 ;if hidden and flagged then show the flag
+ SET C, 11
+
+:ingame_continue
+SET J, tiletypes_left
+ADD J, C
+SET [B], [J]
+
+ADD B, 1
+
+SET J, tiletypes_right
+ADD J, C
+SET [B], [J]
+
+ADD I, 1
+ADD X, 1
+ADD A, 1
+ADD Z, 1
+IFG X, 8
+ ADD Y, 1
+IFG X, 8
+ SET X, 0
+IFG 81, I
+ SET PC, redraw_loop ; loop until whole thing is done
+
+JSR redefine_selection ; the selected tile may have changed
+JSR change_selection
+
+ADD [moves], 1
+
+SET PC, POP
+
+:moves DAT 0
+
+:redefine_selection
+SET B, [select_x]
+SET C, [select_y]
+MUL B, 2
+ADD B, 7 ;change board pos to screen pos
+ADD C, 2
+MUL C, 32
+ADD B, C
+ADD B, 0x8000 ;move to video ram
+SET [old_select_tile1], [B]
+ADD B, 1
+SET [old_select_tile2], [B]
+SET PC, POP
+
+:check_surroundings ;i had to fix endless bugs with this
+SET Z, 1
+
+ADD X, 1 ;check right
+ADD A, 1
+ JSR checkblock
+SUB Y, 1 ;check up-right
+SUB A, 9
+ JSR checkblock
+SUB X, 1 ;check up
+SUB A, 1
+ JSR checkblock
+SUB X, 1 ;check up-left
+SUB A, 1
+ JSR checkblock
+ADD Y, 1 ;check left
+ADD A, 9
+ JSR checkblock
+ADD Y, 1 ;check down-left
+ADD A, 9
+ JSR checkblock
+ADD X, 1 ;check down
+ADD A, 1
+ JSR checkblock
+ADD X, 1 ;check down-right
+ADD A, 1
+ JSR checkblock
+SUB X, 1
+SUB Y, 1
+SUB A, 10 ;back to center
+SET [A], Z ;set tile type
+IFG 2, Z
+ JSR countsurroundings ;if this isn't a number then every surrounding block becomes visible
+SET PC, POP
+
+:countsurroundings
+ADD X, 1 ;check right
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+SUB Y, 1 ;check up-right
+SUB A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+SUB X, 1 ;check up
+SUB A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+SUB X, 1 ;check up-left
+SUB A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+ADD Y, 1 ;check left
+ADD A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+ADD Y, 1 ;check down-left
+ADD A, 9
+IFE [A], 0
+ JSR add_to_surroundings
+ADD X, 1 ;check down
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+ADD X, 1 ;check down-right
+ADD A, 1
+IFE [A], 0
+ JSR add_to_surroundings
+
+SET PC, POP
+
+:add_to_surroundings
+ADD I, 1
+IFG X, 8 ;illegal block
+ SET PC, POP
+IFG Y, 8 ;illegal block
+ SET PC, POP
+SET B, A
+SUB B, board
+ADD B, surrounding_next ;change from board id into surroundings id
+SET [B], 1 ;1 means active
+SET PC, POP
+
+:checkblock
+IFG X, 8 ;illegal block
+ SET PC, POP
+IFG Y, 8 ;illegal block
+ SET PC, POP
+IFE [A], 10
+ ADD Z, 1 ;increment block number
+SET PC, POP
+
+:lose
+SET A, wtf_top
+SET B, 3
+SET C, 0x800E
+JSR drawtiles
+
+SET A, wtf_btm
+SET B, 3
+SET C, 0x802E
+JSR drawtiles
+
+SET A, tiletypes_left
+ADD A, 10
+SET [A], 0x4C33
+
+SET A, tiletypes_right
+ADD A, 10
+SET [A], 0x4C34
+
+JSR redraw_board
+
+SET [gamestate], 2 ;2 is lose
+
+SET PC, POP ;????
+
+
+:generate_board
+SET PUSH, A
+SET PUSH, B
+SET PUSH, C
+SET PUSH, X
+SET PUSH, I ;iterator
+SET I, 0
+
+:g_board_loop
+ADD [timer], 1
+SET A, [timer]
+MUL A, A ;entropy
+MOD A, 9 ;a is x of random mine placement
+SET B, [timer]
+ADD B, 317 ;b is y of mine placement, needs to be very different from A
+MUL B, B
+MOD B, 9
+
+SET C, A ;check if in proximity to cursor, if yes then position again. x
+SUB C, [select_x]
+ADD C, 10 ;work around underflow
+SET X, 0 ;used for checking and condition
+IFG C, 8
+ ADD X, 1
+IFG 12, C
+ ADD X, 1
+
+SET C, B ;do it for the y placement too
+SUB C, [select_y]
+ADD C, 10 ;work around underflow
+IFG C, 8
+ ADD X, 1
+IFG 12, C
+ ADD X, 1
+
+
+IFE X, 4
+ SET PC g_board_loop
+
+MUL B, 9
+ADD A, B
+SET C, board
+ADD C, A
+IFE [C], 10 ;defined at top
+ SET PC g_board_loop ;already a mine there
+SET [C], 10
+
+ADD I, 1
+IFG [mines], I
+ SET PC g_board_loop ;not enough mines
+
+SET [timer], 0
+
+SET I, POP
+SET X, POP
+SET C, POP
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:old_select_operation
+SET [A], [old_select_tile1]
+ADD A, 1 ;and the other side
+SET [A], [old_select_tile2]
+SET PC, POP
+
+:change_selection
+SET PUSH, A ; x value
+SET PUSH, B ; y value
+SET A, [old_select_x]
+SET B, [old_select_y]
+JSR get_selection
+
+IFN [old_select_x], 10 ;10 is the number which tells it not to do anything
+ JSR old_select_operation
+
+SET A, [select_x]
+SET B, [select_y]
+JSR get_selection
+
+SET [old_select_tile1], [A]
+SHL [A], 4 ;remove highest 2 bits
+SHR [A], 4
+BOR [A], 0x2A00
+ADD A, 1 ;and the other side
+SET [old_select_tile2], [A]
+SHL [A], 4 ;remove highest 2 bits
+SHR [A], 4
+BOR [A], 0x2A00
+
+SET [old_select_x], [select_x]
+SET [old_select_y], [select_y]
+
+SET B, POP
+SET A, POP
+SET PC, POP
+
+:get_selection
+ADD B, 2
+MUL A, 2
+ADD A, 7 ;change x and y into screen position instead of game position
+MUL B, 32 ;multiply y by screen width to get memory offset
+ADD A, B ;the final offset moves into A
+ADD A, 0x8000 ;now it's the memory address, wasn't that fun
+SET PC, POP
+
+:old_select_x DAT 10 ;doesn't exist
+:old_select_y DAT 10 ;doesn't exist
+:old_select_tile1 DAT 10 ;ok that does exist but you get the point
+:old_select_tile2 DAT 10
+:select_x DAT 4
+:select_y DAT 4
+
+:timer DAT 0
+:game_started DAT 0
+
+:flag DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+
+:board DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;10 for mines, 0 for undiscovered, 1-9 for selected, 11 for flagged.
+
+:surrounding_work DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;working table, reads from
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
+
+:surrounding_next DAT 0, 0, 0, 0, 0, 0, 0, 0, 0 ;next table, writes to
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0
+DAT 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 ;board of surrounding tiles because my last idea overflowed all the way into the graphics :@
+
+:end
+SET PC, end
+
+:drawtiles
+SET PUSH, I
+SET PUSH, C
+SET I, 0
+
+:drawtiles_loop
+SET [C], [A]
+ADD C, 1
+ADD A, 1
+ADD I, 1
+IFG I, B
+ SET PC, drawtiles_end
+SET PC, drawtiles_loop
+
+:drawtiles_end
+SET C, POP
+SET I, POP
+SET PC, POP
+
+;tilemap data for the main map
+
+:norm_top DAT 0x6F03, 0x6F04, 0x6F05, 0x6F06
+:norm_btm DAT 0x6F07, 0x6F08, 0x6F09, 0x6F0A
+
+:surprised_top DAT 0x6F35, 0x6F36, 0x6F37, 0x6F38
+:surprised_btm DAT 0x6F39, 0x6F3A, 0x6F3B, 0x6F3C
+
+:wtf_top DAT 0x6F3D, 0x6F3E, 0x6F3F, 0x6F40
+:wtf_btm DAT 0x6F41, 0x6F42, 0x6F43, 0x6F44
+
+:boardtiles_l1 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x6F03, 0x6F04, 0x6F05, 0x6F06, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l2 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70B, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x6F07, 0x6F08, 0x6F09, 0x6F0A, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x711, 0x70C, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l3 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l4 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l5 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l6 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l7 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l8 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_l9 DAT 0x8720, 0x8713, 0x8714, 0x8715, 0x8716, 0x8717, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x8718, 0x8719, 0x871A, 0x871B, 0x871C, 0x8720
+
+:boardtiles_20 DAT 0x8720, 0x871D, 0x8F20, 0x8F20, 0x8F20, 0x8710, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x871D, 0x8F20, 0x8F20, 0x8F20, 0x8710, 0x8720
+
+:boardtiles_21 DAT 0x8720, 0x870D, 0x8712, 0x8712, 0x8712, 0x870E, 0x70F, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x8701, 0x8702, 0x710, 0x870D, 0x8712, 0x8712, 0x8712, 0x870E, 0x8720
+
+:boardtiles_22 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70D, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x70E, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+:boardtiles_22 DAT 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x70D, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x712, 0x70E, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720, 0x8720
+
+
+; Tiles.
+
+:tile0 DAT 0xff83, 0x45a9
+:tile1 DAT 0x51b9, 0x7dff
+:tile2 DAT 0xff01, 0x1c1
+:tile3 DAT 0x7151, 0xc9c9
+:tile4 DAT 0x49c9, 0xd171
+:tile5 DAT 0xc101, 0x1ff
+:tile6 DAT 0xff80, 0x8083
+:tile7 DAT 0x8c89, 0x9292
+:tile8 DAT 0x9290, 0x888c
+:tile9 DAT 0x8380, 0x80ff
+:tile10 DAT 0x0, 0x80
+:tile11 DAT 0x8000, 0x0
+:tile12 DAT 0x0, 0x1
+:tile13 DAT 0x100, 0x0
+:tile14 DAT 0x0, 0x55ff
+:tile15 DAT 0xff00, 0x0
+:tile16 DAT 0xc080, 0xc080
+:tile17 DAT 0x101, 0x101
+:tile18 DAT 0x3c, 0x888
+:tile19 DAT 0xbc80, 0xbc80
+:tile20 DAT 0xbc88, 0x90bc
+:tile21 DAT 0x80bc, 0xaca4
+:tile22 DAT 0x8028, 0x2414
+:tile23 DAT 0x0, 0x84
+:tile24 DAT 0xbc84, 0x80bc
+:tile25 DAT 0x80bc, 0x8888
+:tile26 DAT 0xbc80, 0xbcac
+:tile27 DAT 0xa400, 0x0
+:tile28 DAT 0x0, 0xff
+:tile29 DAT 0xff01, 0x101
+:tile30 DAT 0x101, 0x101
+:tile31 DAT 0x0, 0x0
+:tile32 DAT 0xff01, 0x149
+:tile33 DAT 0x7d41, 0x101
+:tile34 DAT 0xff01, 0x149
+:tile35 DAT 0x6559, 0x101
+:tile36 DAT 0xff01, 0x145
+:tile37 DAT 0x5539, 0x101
+:tile38 DAT 0xff01, 0x11d
+:tile39 DAT 0x117d, 0x101
+:tile40 DAT 0xff01, 0x15d
+:tile41 DAT 0x5525, 0x101
+:tile42 DAT 0xff01, 0x139
+:tile43 DAT 0x5525, 0x101
+:tile44 DAT 0xff01, 0x145
+:tile45 DAT 0x350d, 0x101
+:tile46 DAT 0xff01, 0x129
+:tile47 DAT 0x5529, 0x101
+:tile48 DAT 0xff83, 0x10d
+:tile49 DAT 0xd7d, 0x183
+:tile50 DAT 0xff01, 0x5539
+:tile51 DAT 0x6d39, 0x5501
+:tile52 DAT 0xff01, 0x1e1
+:tile53 DAT 0x3131, 0xa969
+:tile54 DAT 0x69a9, 0x7171
+:tile55 DAT 0xa101, 0x1ff
+:tile56 DAT 0xff80, 0x8083
+:tile57 DAT 0x8c88, 0x9096
+:tile58 DAT 0x9690, 0x888c
+:tile59 DAT 0x8380, 0x80ff
+:tile60 DAT 0xff01, 0x1c1
+:tile61 DAT 0x3151, 0xc949
+:tile62 DAT 0x2999, 0x1131
+:tile63 DAT 0xc101, 0x1ff
+:tile64 DAT 0xff80, 0x8083
+:tile65 DAT 0xcc88, 0xd0de
+:tile66 DAT 0x9ed0, 0xc8cc
+:tile67 DAT 0x8380, 0x80ff
+:tile68 DAT 0x48, 0x7c40
+:tile69 DAT 0x48, 0x6458
+:tile70 DAT 0x44, 0x5438
+:tile71 DAT 0x1c, 0x107c
+:tile72 DAT 0x5c, 0x5424
+:tile73 DAT 0x38, 0x5424
+:tile74 DAT 0x44, 0x340c
+:tile75 DAT 0x28, 0x5428
+:tile76 DAT 0x48, 0x5438
+:tile77 DAT 0x38, 0x4438
+:tile78 DAT 0x18, 0xe018
+:tile79 DAT 0x60, 0x9060
+:tile80 DAT 0x70, 0x80f0
+:tile81 DAT 0x0, 0xf8
+:tile82 DAT 0x8040, 0x80f8
+:tile83 DAT 0xe8, 0xf0
+:tile84 DAT 0x20e0, 0x0
+:tile85 DAT 0xf8, 0x8080
+:tile86 DAT 0x60, 0x9060
+:tile87 DAT 0xb0, 0x90d0
+:tile88 DAT 0xf0, 0x90b0
+
+;1kb worth of tiles used for the intro sequence. worth it!
+
+
+:intro0 DAT 0x0, 0x101
+:intro1 DAT 0xc3eb, 0xebeb
+:intro2 DAT 0xebea, 0xe8e0
+:intro3 DAT 0xc000, 0x80e0
+:intro4 DAT 0xe0e8, 0xeaeb
+:intro5 DAT 0xebeb, 0xb01
+:intro6 DAT 0x101, 0x0
+:intro7 DAT 0x1, 0x1e1
+:intro8 DAT 0xebeb, 0xebeb
+:intro9 DAT 0xeb0b, 0x101
+:intro10 DAT 0x100, 0x101
+:intro11 DAT 0xc1eb, 0xebeb
+:intro12 DAT 0xebea, 0xe8eb
+:intro13 DAT 0xe3eb, 0x2b03
+:intro14 DAT 0x101, 0x0
+:intro15 DAT 0x101, 0xc1eb
+:intro16 DAT 0xebeb, 0xebeb
+:intro17 DAT 0xeb43, 0xc1c1
+:intro18 DAT 0x303, 0xb00
+:intro19 DAT 0x0, 0x0
+:intro20 DAT 0xf0f0, 0x3030
+:intro21 DAT 0xc0c, 0xc0c
+:intro22 DAT 0xc0c, 0xc0c
+:intro23 DAT 0x3030, 0xf0f0
+:intro24 DAT 0x303, 0x3333
+:intro25 DAT 0x303, 0x303
+:intro26 DAT 0xf0f, 0xf0f
+:intro27 DAT 0x303, 0xf0f
+:intro28 DAT 0xf0f, 0x303
+:intro29 DAT 0x1818, 0x1818
+:intro30 DAT 0x7878, 0x7878
+:intro31 DAT 0x1818, 0x7878
+:intro32 DAT 0x4040, 0x6078
+:intro33 DAT 0x7f61, 0x434f
+:intro34 DAT 0x1f7f, 0x3f1f
+:intro35 DAT 0x4f47, 0x417f
+:intro36 DAT 0x7f7f, 0x7f7f
+:intro37 DAT 0x7f6f, 0x4040
+:intro38 DAT 0x0, 0x40
+:intro39 DAT 0x4040, 0x787f
+:intro40 DAT 0x7f7f, 0x7f7f
+:intro41 DAT 0x4740, 0x4040
+:intro42 DAT 0x40, 0x6070
+:intro43 DAT 0x7f71, 0x6347
+:intro44 DAT 0xf1f, 0x3f7f
+:intro45 DAT 0x7f0f, 0x0
+:intro46 DAT 0x0, 0x4040
+:intro47 DAT 0x4060, 0x7f7f
+:intro48 DAT 0x7f7f, 0x7f7f
+:intro49 DAT 0x4340, 0x6073
+:intro50 DAT 0x703c, 0x600
+:intro51 DAT 0x0, 0xffff
+:intro52 DAT 0x0, 0x3030
+:intro53 DAT 0xc3c3, 0xc0c0
+:intro54 DAT 0xc0c0, 0x303
+:intro55 DAT 0x0, 0x0
+:intro56 DAT 0xffff, 0x0
+:intro57 DAT 0xf0f, 0xc0c
+:intro58 DAT 0x3030, 0x3030
+:intro59 DAT 0x3030, 0x3030
+:intro60 DAT 0xc0c, 0xf0f
+:intro61 DAT 0x7878, 0x1818
+:intro62 DAT 0xc0c0, 0xc0c0
+:intro63 DAT 0xc0c0, 0xc0c0
+:intro64 DAT 0xc0d0, 0xd4d4
+:intro65 DAT 0xd2d2, 0xc2c6
+:intro66 DAT 0xc6d6, 0x601
+:intro67 DAT 0x0, 0x216
+:intro68 DAT 0xd6d6, 0xd6d6
+:intro69 DAT 0xd6d2, 0x12d0
+:intro70 DAT 0xd0d0, 0xd4d4
+:intro71 DAT 0xd486, 0x284
+:intro72 DAT 0x4416, 0x602
+:intro73 DAT 0x2, 0x2d6
+:intro74 DAT 0xd6d6, 0xd6d6
+:intro75 DAT 0xd686, 0x8282
+:intro76 DAT 0x616, 0x0
+:intro77 DAT 0x2, 0x2d6
+:intro78 DAT 0xd6d6, 0xd6d6
+:intro79 DAT 0xd686, 0x8282
+:intro80 DAT 0x616, 0x0
+:intro81 DAT 0x2, 0x2d6
+:intro82 DAT 0xd6d6, 0xd6d6
+:intro83 DAT 0xd602, 0x2d6
+:intro84 DAT 0xd400, 0x2
+:intro85 DAT 0x2d6, 0xd6d6
+:intro86 DAT 0xd6d6, 0xd686
+:intro87 DAT 0x8282, 0x616
+:intro88 DAT 0x0, 0x0
+:intro89 DAT 0x202, 0xd6d6
+:intro90 DAT 0xd6d6, 0xd6d6
+:intro91 DAT 0x606, 0x8454
+:intro92 DAT 0xcfcf, 0xcfcf
+:intro93 DAT 0xc0c0, 0xc0c0
+:intro94 DAT 0xc0c0, 0xc0c0
+:intro95 DAT 0x303, 0x303
+:intro96 DAT 0xfb67, 0xcf8f
+:intro97 DAT 0x9f9f, 0x9f7f
+:intro98 DAT 0x7f1f, 0x700
+:intro99 DAT 0x0, 0x0
+:intro100 DAT 0xf7f, 0xffff
+:intro101 DAT 0x3f1f, 0xe03
+:intro102 DAT 0xf3f, 0xff7f
+:intro103 DAT 0x3f0f, 0x601
+:intro104 DAT 0x0, 0x80
+:intro105 DAT 0x8080, 0xf0ff
+:intro106 DAT 0xffff, 0xffff
+:intro107 DAT 0x87c0, 0xc3e7
+:intro108 DAT 0xf00c, 0x80
+:intro109 DAT 0x8080, 0xf0ff
+:intro110 DAT 0xffff, 0xffff
+:intro111 DAT 0x87c0, 0xc3e7
+:intro112 DAT 0xf00c, 0x80
+:intro113 DAT 0x80c0, 0xfeff
+:intro114 DAT 0xffff, 0xffff
+:intro115 DAT 0x8282, 0x301
+:intro116 DAT 0x80, 0x8080
+:intro117 DAT 0xf0ff, 0xffff
+:intro118 DAT 0xffff, 0x87c0
+:intro119 DAT 0xc3e7, 0xf00c
+:intro120 DAT 0x0, 0x8080
+:intro121 DAT 0xc0fe, 0xffff
+:intro122 DAT 0xffff, 0xbf81
+:intro123 DAT 0x83ff, 0xfffe
+:intro124 DAT 0xc3c3, 0xf0f
+:intro125 DAT 0x0, 0x303
+:intro126 DAT 0x303, 0x0
+:intro127 DAT 0x0, 0x0
+
+
+
+:storetiles ;A = Data to Read, B = Length, C = Destination, I = Iterator
+SET PUSH, I
+MUL B, 2 ;2 words to a character
+SET I, 0
+
+:storetiles_loop
+SET [C], [A]
+ADD A, 1
+ADD I, 1
+ADD C, 1
+IFE I, B
+ SET PC, storetiles_end
+SET PC, storetiles_loop
+
+:storetiles_end
+SET I, POP
+SET PC, POP
diff --git a/examples/pix.s b/examples/pix.s
new file mode 100644
index 0000000..785da35
--- /dev/null
+++ b/examples/pix.s
@@ -0,0 +1,74 @@
+; pixel painting API with small demo loop
+; author: twitter @blubdidub
+
+jsr init_pixelmode
+
+set x, 0
+set y, 0
+set c, 0x5
+
+:demo_loop
+ set a, x
+ set b, y
+ jsr setpixel
+ add x, 1
+ ifg 0x20, x
+ set PC, cont
+ set x, 0
+ add y, 1
+ ifg 0x18, y
+ set PC, cont
+ set y, 0
+ add c, 1
+ ifg c, 0xe
+ set c, 0
+:cont
+
+ set j, 0
+:wait_loop
+ add j, 1
+ ifg 30, j
+ set PC, wait_loop
+
+ set pc, demo_loop
+
+; initialize pixel_mode for character 0x00
+:init_pixelmode
+ set [0x8180], 0x0f0f
+ set [0x8181], 0x0f0f
+ set pc, POP
+
+; set pixel to color (24*32 pixels)
+; param A: x position
+; param B: y position
+; param C: color
+:setpixel
+ set PUSH, y
+ set PUSH, i
+ set PUSH, c
+ ; calculate character address
+ set y, b
+ shr y, 1
+ ifn o, 0x0
+ set PC, lower_pixel
+ ; upper pixel
+ mul y, 0x20
+ add y, a
+ set i, [0x8000+y] ; read current val
+ and i, 0x0f00 ; clear upper pixel
+ shl c, 12
+ bor i, c ; set new color
+ set PC, finish_pixel
+:lower_pixel
+ mul y, 0x20
+ add y, a
+ set i, [0x8000+y] ; read current val
+ and i, 0xf000 ; clear lower pixel
+ shl c, 8
+ bor i, c ; set new color
+:finish_pixel
+ set [0x8000+y], i ; set character
+ set c, POP
+ set i, POP
+ set y, POP
+ set PC, POP
diff --git a/examples/spec_example.asm b/examples/spec_example.asm
new file mode 100644
index 0000000..f5f86de
--- /dev/null
+++ b/examples/spec_example.asm
@@ -0,0 +1,30 @@
+; example from Notch's spec for DCPU-16
+
+; Try some basic stuff
+ SET A, 0x30 ; 7c01 0030
+ SET [0x1000], 0x20 ; 7de1 1000 0020
+ SUB A, [0x1000] ; 7803 1000
+ IFN A, 0x10 ; c00d
+ SET PC, crash ; 7dc1 001a [*]
+
+; Do a loopy thing
+ SET I, 10 ; a861
+ SET A, 0x2000 ; 7c01 2000
+:loop SET [0x2000+I], [A] ; 2161 2000
+ SUB I, 1 ; 8463
+ IFN I, 0 ; 806d
+ SET PC, loop ; 7dc1 000d [*]
+
+; Call a subroutine
+ SET X, 0x4 ; 9031
+ JSR testsub ; 7c10 0018 [*]
+ SET PC, crash ; 7dc1 001a [*]
+
+:testsub SHL X, 4 ; 9037
+ SET PC, POP ; 61c1
+
+; Hang forever. X should now be 0x40 if everything went right.
+:crash SET PC, crash ; 7dc1 001a [*]
+
+; [*]: Note that these can be one word shorter and one cycle faster by using the short form (0x00-0x1f) of literals,
+; but my assembler doesn't support short form labels yet.
diff --git a/examples/t.d b/examples/t.d
new file mode 100644
index 0000000..1a6255f
--- /dev/null
+++ b/examples/t.d
@@ -0,0 +1,125 @@
+7c01 8022 7c11 01e6 7c10 005f 7c01 8042
+7c11 01f7 7c10 005f 8061 8462 7c10 0070
+800c 7dc1 000d 7de4 006f 8dd9 01e2 006f
+7de4 006f 8dd9 19e2 006f 7c01 8000 8081
+8402 01fe 8180 7dc1 001f 7c01 8000 8011
+7c81 0700 8402 8412 06ae 7dc1 0031 8011
+d802 01fe 8180 7dc1 0028 7c21 f700 7c01
+8021 7c11 020a 7c10 005f 7c01 8061 7c11
+0211 7c10 005f 7c01 80a1 7c11 0218 7c10
+005f 7c01 8000 7d01 000a 0100 7d01 001f
+0100 7c02 0020 01fe 8180 7dc1 004b 7c10
+008e 7c10 009d 7c10 007e 7dc1 013e 809c
+61c1 2481 088a 8402 8412 7dc1 005f 7de4
+006f 274d 85e2 006f 7801 006f 61c1 c00f
+7811 007d 7c12 9000 2401 800c 61c1 8091
+85e2 007d bde9 007d 61c1 0000 7c10 0067
+9c06 79e1 008a 008c 01e1 008c 7de1 008d
+8011 61c1 0000 0000 0000 8011 7801 01e5
+7c21 8048 0011 a816 7c1a e730 04a1 8423
+a805 800e 7dc1 0092 61c1 7801 01e4 7c21
+8088 0011 a816 7c1a e730 04a1 8423 a805
+800e 7dc1 00a1 61c1 7831 008d 7841 008a
+8847 784a 008b 9047 7c42 023c 8061 3011
+801c 7dc1 00cb 022e 7dc1 00c6 7c3e 8180
+8dc2 80bc 7dc1 00cb 8011 61c1 800c 8011
+04b1 0531 0001 8462 8832 8442 8411 c06c
+61c1 8c6f 7dc1 00b7 e032 7dc1 00b7 7c31
+80e1 7841 008c 9847 7c42 023c 8061 3011
+801c 7c11 0700 04b1 0531 0001 8462 8832
+8442 8411 c06c 61c1 8c6f 7dc1 00df e032
+7dc1 00df 7c01 816b 0011 8071 8061 0431
+80bc 7dc1 010a 8832 8462 1aae 7dc1 00f8
+8472 7c13 0020 907c 7dc1 010a 7c1e 8000
+7dc1 00f6 040c 7dc1 0117 8061 2481 8462
+8402 8412 1b4e 7dc1 010e d003 d013 7c03
+0020 01fe 8000 7dc1 0128 7c13 0020 907c
+7dc1 010a 05fe 8000 7dc1 010a 7dc1 00f6
+807c 61c1 1de2 01e3 5c71 01de 7874 01e4
+1de2 01e5 7c10 008e 7aae 01e3 61c1 81e1
+01e3 85e2 01e4 7c10 009d 61c1 7c10 007e
+8801 7c10 00ac 801c 7dc1 01b4 7c10 00d7
+7801 01e4 41e1 01c9 01ca 7851 01c9 8401
+7c10 00ac 805c 7dc1 019e 8453 7c10 0070
+840c 7dc1 0166 880c 7dc1 0175 8c0c 7dc1
+018b 900c 7dc1 0184 7dc1 0152 8001 7c10
+00ac 89e3 008d 8801 7c10 00ac 841c 7dc1
+014f 89e2 008d 7dc1 014f 8001 7c10 00ac
+89e2 008d 8801 7c10 00ac 841c 7dc1 014f
+89e3 008d 7dc1 014f 7de1 01c9 0064 7c51
+0064 7dc1 0152 8001 7c10 00ac 85e2 008b
+8de9 008b 8801 7c10 00ac 841c 7dc1 014f
+85e3 008b 8de9 008b 7dc1 014f 8001 7c10
+00ac 7de2 008d 0020 8801 7c10 00ac 841c
+7dc1 014d 7de3 008d 0020 8401 7c10 00ac
+7c10 00f2 7dc1 013e 7c21 f480 7c01 8089
+7c11 021e 7c10 005f 7c01 80a9 7c11 022d
+7c10 005f 7c01 80c9 7c11 021e 7c10 005f
+85c3 09c4 09c4 09c4 07d0 05dc 03e8 0320
+028a 01f4 012c 00c8 0064 004b 0032 001e
+0014 000a 0005 0003 0002 0001 0000 0002
+0005 000f 003c 0000 0001 0000 0050 0072
+0065 0073 0073 0020 0061 006e 0079 0020
+006b 0065 0079 0020 0074 006f 0000 0020
+006d 0061 006b 0065 0020 0067 0061 006d
+0065 0020 0072 0061 006e 0064 006f 006d
+003a 0000 0053 0063 006f 0072 0065 003a
+0000 004c 0065 0076 0065 006c 003a 0000
+004e 0065 0078 0074 003a 0000 0020 0020
+0020 0020 0020 0020 0020 0020 0020 0020
+0020 0020 0020 0020 0000 0020 0020 0047
+0061 006d 0065 0020 004f 0076 0065 0072
+0021 0020 0020 0000 0000 0c00 0000 0000
+0000 0c00 0000 0000 0000 0c00 0000 0000
+0000 0c00 0000 0000 0000 0000 0000 0000
+0c00 0c00 0c00 0c00 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0c00 0000 0000
+0000 0c00 0000 0000 0000 0c00 0000 0000
+0000 0c00 0000 0000 0000 0000 0000 0000
+0c00 0c00 0c00 0c00 0000 0000 0000 0000
+0000 0000 0000 0000 0d00 0000 0000 0000
+0d00 0d00 0d00 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0d00 0d00 0000
+0000 0d00 0000 0000 0000 0d00 0000 0000
+0d00 0d00 0d00 0000 0000 0000 0d00 0000
+0000 0000 0000 0000 0000 0d00 0000 0000
+0000 0d00 0000 0000 0d00 0d00 0000 0000
+0000 0000 0000 0000 0000 0000 0e00 0000
+0e00 0e00 0e00 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0e00 0000 0000
+0000 0e00 0000 0000 0000 0e00 0e00 0000
+0e00 0e00 0e00 0000 0e00 0000 0000 0000
+0000 0000 0000 0000 0e00 0e00 0000 0000
+0000 0e00 0000 0000 0000 0e00 0000 0000
+0000 0000 0000 0000 0b00 0b00 0000 0000
+0b00 0b00 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0b00 0b00 0000 0000
+0b00 0b00 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0b00 0b00 0000 0000
+0b00 0b00 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0b00 0b00 0000 0000
+0b00 0b00 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0900 0900 0000
+0900 0900 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0900 0000 0000
+0000 0900 0900 0000 0000 0000 0900 0000
+0000 0000 0000 0000 0000 0900 0900 0000
+0900 0900 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0900 0000 0000
+0000 0900 0900 0000 0000 0000 0900 0000
+0000 0000 0000 0000 0800 0800 0800 0000
+0000 0800 0000 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0800 0000 0000
+0800 0800 0000 0000 0000 0800 0000 0000
+0000 0000 0000 0000 0000 0800 0000 0000
+0800 0800 0800 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0800 0000 0000
+0000 0800 0800 0000 0000 0800 0000 0000
+0000 0000 0000 0000 0a00 0a00 0000 0000
+0000 0a00 0a00 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0000 0a00 0000
+0000 0a00 0a00 0000 0000 0a00 0000 0000
+0000 0000 0000 0000 0a00 0a00 0000 0000
+0000 0a00 0a00 0000 0000 0000 0000 0000
+0000 0000 0000 0000 0000 0000 0a00 0000
+0000 0a00 0a00 0000 0000 0a00 0000 0000
diff --git a/examples/t.s b/examples/t.s
new file mode 100644
index 0000000..4481668
--- /dev/null
+++ b/examples/t.s
@@ -0,0 +1,2 @@
+; retrieve some entropy...
+ SET A, 0x8000 + 32 * 1 + 2
diff --git a/examples/t2.d b/examples/t2.d
new file mode 100644
index 0000000..880be61
--- /dev/null
+++ b/examples/t2.d
@@ -0,0 +1,72 @@
+7c01 8022 7c11 01e6 7c10 005f 7c01 8042
+7c11 01f7 7c10 005f 8061 8462 7c10 0070
+800c 7dc1 000d 7de4 006f 8dd9 01e2 006f
+7de4 006f 8dd9 19e2 006f 7c01 8000 8081
+8402 01fe 8180 7dc1 001f 7c01 8000 8011
+7c81 0700 8402 8412 06ae 7dc1 0031 8011
+d802 01fe 8180 7dc1 0028 7c21 f700 7c01
+8021 7c11 020a 7c10 005f 7c01 8061 7c11
+0211 7c10 005f 7c01 80a1 7c11 0218 7c10
+005f 7c01 8000 7d01 000a 0100 7d01 001f
+0100 7c02 0020 01fe 8180 7dc1 004b 7c10
+008e 7c10 009d 7c10 007e 7dc1 013e 809c
+61c1 2481 088a 8402 8412 7dc1 005f 7de4
+006f 274d 85e2 006f 7801 006f 61c1 c00f
+7811 007d 7c12 9000 2401 800c 61c1 8091
+85e2 007d bde9 007d 61c1 0000 7c10 0067
+9c06 79e1 008a 008c 01e1 008c 7de1 008d
+8011 61c1 0000 0000 0000 8011 7801 01e5
+7c21 8048 0011 a816 7c1a e730 04a1 8423
+a805 800e 7dc1 0092 61c1 7801 01e4 7c21
+8088 0011 a816 7c1a e730 04a1 8423 a805
+800e 7dc1 00a1 61c1 7831 008d 7841 008a
+8847 784a 008b 9047 7c42 023c 8061 3011
+801c 7dc1 00cb 022e 7dc1 00c6 7c3e 8180
+8dc2 80bc 7dc1 00cb 8011 61c1 800c 8011
+04b1 0531 0001 8462 8832 8442 8411 c06c
+61c1 8c6f 7dc1 00b7 e032 7dc1 00b7 7c31
+80e1 7841 008c 9847 7c42 023c 8061 3011
+801c 7c11 0700 04b1 0531 0001 8462 8832
+8442 8411 c06c 61c1 8c6f 7dc1 00df e032
+7dc1 00df 7c01 816b 0011 8071 8061 0431
+80bc 7dc1 010a 8832 8462 1aae 7dc1 00f8
+8472 7c13 0020 907c 7dc1 010a 7c1e 8000
+7dc1 00f6 040c 7dc1 0117 8061 2481 8462
+8402 8412 1b4e 7dc1 010e d003 d013 7c03
+0020 01fe 8000 7dc1 0128 7c13 0020 907c
+7dc1 010a 05fe 8000 7dc1 010a 7dc1 00f6
+807c 61c1 1de2 01e3 5c71 01de 7874 01e4
+1de2 01e5 7c10 008e 7aae 01e3 61c1 81e1
+01e3 85e2 01e4 7c10 009d 61c1 7c10 007e
+8801 7c10 00ac 801c 7dc1 01b4 7c10 00d7
+7801 01e4 41e1 01c9 01ca 7851 01c9 8401
+7c10 00ac 805c 7dc1 019e 8453 7c10 0070
+840c 7dc1 0166 880c 7dc1 0175 8c0c 7dc1
+018b 900c 7dc1 0184 7dc1 0152 8001 7c10
+00ac 89e3 008d 8801 7c10 00ac 841c 7dc1
+014f 89e2 008d 7dc1 014f 8001 7c10 00ac
+89e2 008d 8801 7c10 00ac 841c 7dc1 014f
+89e3 008d 7dc1 014f 7de1 01c9 0064 7c51
+0064 7dc1 0152 8001 7c10 00ac 85e2 008b
+8de9 008b 8801 7c10 00ac 841c 7dc1 014f
+85e3 008b 8de9 008b 7dc1 014f 8001 7c10
+00ac 7de2 008d 0020 8801 7c10 00ac 841c
+7dc1 014d 7de3 008d 0020 8401 7c10 00ac
+7c10 00f2 7dc1 013e 7c21 f480 7c01 8089
+7c11 021e 7c10 005f 7c01 80a9 7c11 022d
+7c10 005f 7c01 80c9 7c11 021e 7c10 005f
+85c3 09c4 09c4 09c4 07d0 05dc 03e8 0320
+028a 01f4 012c 00c8 0064 004b 0032 001e
+0014 000a 0005 0003 0002 0001 0000 0002
+0005 000f 003c 0000 0001 0000 0050 0072
+0065 0073 0073 0020 0061 006e 0079 0020
+006b 0065 0079 0020 0074 006f 0000 0020
+006d 0061 006b 0065 0020 0067 0061 006d
+0065 0020 0072 0061 006e 0064 006f 006d
+003a 0000 0053 0063 006f 0072 0065 003a
+0000 004c 0065 0076 0065 006c 003a 0000
+004e 0065 0078 0074 003a 0000 0020 0020
+0020 0020 0020 0020 0020 0020 0020 0020
+0020 0020 0020 0020 0000 0020 0020 0047
+0061 006d 0065 0020 004f 0076 0065 0072
+0021 0020 0020 0000 0000 0c00 0000 0000
diff --git a/examples/test.dump b/examples/test.dump
new file mode 100644
index 0000000..3f3fdad
--- /dev/null
+++ b/examples/test.dump
@@ -0,0 +1,4 @@
+0000: 7c01 0030 7de1 1000 0020 7803 1000 c00d
+0008: 7dc1 001a a861 7c01 2000 2161 2000 8463
+0010: 806d 7dc1 000d 9031 7c10 0018 7dc1 001a
+0018: 9037 61c1 7dc1 001a 0000 0000 0000 0000
diff --git a/examples/test.s b/examples/test.s
new file mode 100644
index 0000000..151e9ad
--- /dev/null
+++ b/examples/test.s
@@ -0,0 +1,28 @@
+; Try some basic stuff
+ SET A, 0x30 ; 7c01 0030
+ SET [0x1000], 0x20 ; 7de1 1000 0020
+ SUB A, [0x1000] ; 7803 1000
+ IFN A, 0x10 ; c00d
+ SET PC, crash ; 7dc1 001a [*]
+
+; Do a loopy thing
+ SET I, 10 ; a861
+ SET A, 0x2000 ; 7c01 2000
+:loop SET [0x2000+I], [A] ; 2161 2000
+ SUB I, 1 ; 8463
+ IFN I, 0 ; 806d
+ SET PC, loop ; 7dc1 000d [*]
+
+; Call a subroutine
+ SET X, 0x4 ; 9031
+ JSR testsub ; 7c10 0018 [*]
+ SET PC, crash ; 7dc1 001a [*]
+
+:testsub SHL X, 4 ; 9037
+ SET PC, POP ; 61c1
+
+; Hang forever. X should now be 0x40 if everything went right.
+:crash SET PC, crash ; 7dc1 001a [*]
+
+; [*]: Note that these can be one word shorter and one cycle faster by using the short form (0x00-0x1f) of literals,
+; but my assembler doesn't support short form labels yet.
diff --git a/examples/test2.s b/examples/test2.s
new file mode 100644
index 0000000..750829e
--- /dev/null
+++ b/examples/test2.s
@@ -0,0 +1,19 @@
+; Assembler test for DCPU
+; by Markus Persson
+
+ set a, 0xbeef ; Assign 0xbeef to register a
+ set [0x1000], a ; Assign memory at 0x1000 to value of register a
+ ifn a, [0x1000] ; Compare value of register a to memory at 0x1000 ..
+ set PC, end ; .. and jump to end if they don't match
+
+ set i, 0 ; Init loop counter, for clarity
+:nextchar
+ ife [data+i], 0 ; If the character is 0 ..
+ set PC, end ; .. jump to the end
+ set [0x8000+i], [data+i] ; Video ram starts at 0x8000, copy char there
+ add i, 1 ; Increase loop counter
+ set PC, nextchar ; Loop
+
+:data dat "Hello world!", 0 ; Zero terminated string
+
+:end sub PC, 1 ; Freeze the CPU forever
diff --git a/examples/test3.s b/examples/test3.s
new file mode 100644
index 0000000..06cd1cf
--- /dev/null
+++ b/examples/test3.s
@@ -0,0 +1,37 @@
+; Notch's second "hello word" program.
+; http://i.imgur.com/XIXc4.jpg
+; Supposed to show formatting.
+:start
+ set i, 0
+ set j, 0
+ set b, 0xf100
+:nextchar
+ set a, [data+i]
+ ife a, 0
+ set PC, end
+ ifg a, 0xff
+ set PC, setcolor
+ bor a, b
+ set [0x8000+j], a
+ add i, 1
+ add j, 1
+ set PC, nextchar
+:setcolor
+ set b, a
+ and b, 0xff
+ shl b, 8
+ ifg a, 0x1ff
+ add b, 0x80 ; Add high bit to each character we color. So the character must be 7-bit ASCII
+ add i, 1
+ set PC, nextchar
+:data
+ dat 0x170, "Hello ", 0x2e1, "world", 0x170, ", how are you?", 0
+ ; Color format:
+ ; After processing:
+ ; 0x170 -> b = 0x7000 -> 0111 0000 0XXX XXXX = white(grey) on black
+ ; 0x2e1 -> b = 0xe180 -> 1110 0001 1XXX XXXX = yellow on blue
+ ; b gets OR'd with each character.
+ ; ANSI says: black is 0, white is 7, yellow is 3, blue is 4
+ ; If black is 0 and grey is 7, it's <FORE> <BACK> <EXTRA BIT> or <FORE> <EXTRA BIT> <BACK>
+:end
+ set PC, start
diff --git a/examples/test4.s b/examples/test4.s
new file mode 100644
index 0000000..4028b78
--- /dev/null
+++ b/examples/test4.s
@@ -0,0 +1,13 @@
+;Testing the display memory, and making pretty colours.
+; By SmokestormX
+set z,0x8000; Starting mem
+set c,0x1041; Yellow A charachter
+set i,0;
+:loop
+set [0x8000+i],c; Set the Display+offset to C
+add i,1;
+add c,0x1100; Add offset to C , change this around.
+ife i,0x200; 0x200 is the exact size of the display.
+set pc,end;
+set pc,loop;
+:end
diff --git a/examples/testx.s b/examples/testx.s
new file mode 100644
index 0000000..b430e23
--- /dev/null
+++ b/examples/testx.s
@@ -0,0 +1,3 @@
+ set A, 0x100
+:data
+ dat "aaa", data, 0x42, 0
diff --git a/examples/x.s b/examples/x.s
new file mode 100644
index 0000000..6411b7e
--- /dev/null
+++ b/examples/x.s
@@ -0,0 +1,15 @@
+set a 33
+
+:loop
+ set [0x8000+i] a
+
+ add a 1
+ add i 1
+
+ ifg i 0x180
+ set i 0
+ ifg a 126
+ set a 33
+
+ set pc loop
+