Created
April 10, 2012 20:04
-
-
Save Blecki/2354098 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Test file for the DCPU-16 compiler. | |
// | |
void set(char chr, int x, int y) | |
{ | |
int mem = 0x8000 + x + y * 32; | |
*mem = chr + 0x7000; | |
} | |
void print(char* msg, int x, int y) | |
{ | |
int i = 0; | |
//while (*(msg + i) != '\0') | |
for (i = 0; *(msg + i) != '\0'; i += 1) | |
set(*(msg + i), x + i, y); | |
} | |
void main() | |
{ | |
print("Hello 0x10c!", 0, 0); | |
print("How do you like", 0, 1); | |
print("my C compiler? :D", 0, 2); | |
} | |
//Ported to DCPUC | |
function set(c, x, y) | |
{ | |
var mem = 0x8000 + x + y * 32; | |
*mem = c + 0x7000; | |
} | |
function print(msg, x, y) | |
{ | |
var i = 0; | |
while (*(msg + i) != 0) | |
{ | |
set(*(msg + i), x + i, y); | |
i = i + 1; | |
} | |
} | |
print("Hello 0x10c!", 0, 0); | |
print("How do you like", 0, 1); | |
print("my C compiler? :D", 0, 2); | |
SET A, L2_DATA | |
SET B, 0x0000 | |
SET C, 0x0000 | |
JSR L1_print | |
SET PUSH, A | |
SET A, L3_DATA | |
SET PUSH, B | |
SET B, 0x0000 | |
SET PUSH, C | |
SET C, 0x0001 | |
JSR L1_print | |
SET C, POP | |
SET B, POP | |
SET A, POP | |
SET PUSH, A | |
SET A, L4_DATA | |
SET PUSH, B | |
SET B, 0x0000 | |
SET PUSH, C | |
SET C, 0x0002 | |
JSR L1_print | |
SET C, POP | |
SET B, POP | |
SET A, POP | |
BRK | |
:L0_set | |
SET Y, C | |
MUL Y, 0x0020 | |
SET Z, B | |
SET X, 0x8000 | |
ADD X, Z | |
ADD X, Y | |
SET Y, X | |
SET Z, A | |
ADD Z, 0x7000 | |
SET [Y], Z | |
SET PC, POP | |
:L1_print | |
SET X, 0x0000 | |
:L0BEGIN_WHILE | |
SET Z, X | |
SET Y, A | |
ADD Y, Z | |
SET Y, [Y] | |
IFN Y, 0x0000 | |
SET PC, L1ELSE | |
SET PC, L2END | |
:L1ELSE | |
SET PUSH, A | |
SET Y, X | |
SET A, PEEK | |
ADD A, Y | |
SET A, [A] | |
SET PUSH, B | |
SET A, X | |
SET B, PEEK | |
ADD B, A | |
SET PUSH, C | |
SET PUSH, X | |
JSR L0_set | |
SET X, POP | |
SET C, POP | |
SET B, POP | |
SET A, POP | |
ADD X, 0x0001 | |
SET PC, L0BEGIN_WHILE | |
:L2END | |
SET PC, POP | |
;DCPUC FUNCTION itoa itoa_FUNCTION 2 number *dest | |
;Writes the digits to *dest, returns end of written buffer. | |
:itoa_FUNCTION | |
IFG A, 0xA | |
SET PC, itoa_THENCLAUSE | |
SET PC, itoa_REST | |
:itoa_THENCLAUSE | |
SET PUSH, A | |
DIV A, 0xA | |
JSR itoa_FUNCTION | |
SET A, POP | |
:itoa_REST | |
MOD A, 0xA | |
ADD A, 0x30 | |
SET [B], A | |
ADD B, 0x1 | |
SET A, B | |
SET PC, POP | |
;DCPUC ENDFUNCTION | |
;DCPUC FUNCTION strlen strlen_FUNCTION 1 *char | |
;Returns the length of the string | |
:strlen_FUNCTION | |
SET B, A | |
SET A, 0x0 | |
:strlen_LOOP | |
IFE [B], 0x0 | |
SET PC, POP | |
ADD A, 0x1 | |
ADD B, 0x1 | |
SET PC, strlen_LOOP | |
;DCPUC ENDFUNCTION | |
;DCPUC FUNCTION strcpy strcpy_FUNCTION 2 *char a, *char b | |
;Copies a into b, does not copy trailing null | |
:strcpy_FUNCTION | |
IFE [A], 0x0 | |
SET PC, strcpy_LEAVE | |
SET [B], [A] | |
ADD B, 0x1 | |
ADD A, 0x1 | |
SET PC, strcpy_FUNCTION | |
:strcpy_LEAVE | |
SET A, B; | |
SET PC, POP; | |
;DCPUC ENDFUNCTION | |
;DCPUC FUNCTION abort dcpuc_abort 1 *char | |
;Prints the message and aborts program | |
:dcpuc_abort | |
SET B, 0x8000 | |
JSR strcpy_FUNCTION | |
BRK | |
;DCPUC ENDFUNCTION | |
;DCPUC STATIC | |
:DCPUC_MR_HEAPHEAD DAT 0x0 | |
:DCPUC_MR_HEAPEND DAT 0x0 | |
:DCPUC_MR_HEAPFREE DAT 0x0 | |
:DCPUC_MR_OUTOFMEMORYERROR DAT "Fatal Error: out of memory" 0x0 | |
;DCPUC END | |
;DCPUC FUNCTION init_mrheap dcpuc_init_mrheap 2 *memstart, *memend | |
;Initializes a mark-and-release heap from memstart to memend | |
:dcpuc_init_mrheap | |
SET [DCPUC_MR_HEAPHEAD], A | |
SET [DCPUC_MR_HEAPFREE], A | |
SET [DCPUC_MR_HEAPEND], B | |
SET PC, POP | |
;DCPUC END | |
;DCPUC FUNCTION mrmalloc dcpuc_mrmalloc 1 length | |
;Returns address of free block of size length | |
:dcpuc_mrmalloc | |
SET C, [DCPUC_MR_HEAPFREE] | |
ADD [DCPUC_MR_HEAPFREE], A | |
IFG [DCPUC_MR_HEAPEND], [DCPUC_MR_HEAPFREE] | |
SET PC, DCPUC_MRMALLOC_0 | |
SET A, DCPUC_MR_OUTOFMEMORYERROR | |
JSR dcpuc_abort ;Never returns | |
:DCPUC_MRMALLOC_0 | |
SET A, C | |
SET PC, POP | |
;DCPUC END | |
:L2_DATA DAT 0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x0020, 0x0030, 0x0078, 0x0031, 0x0030, 0x0063, 0x0021 | |
:L3_DATA DAT 0x0048, 0x006F, 0x0077, 0x0020, 0x0064, 0x006F, 0x0020, 0x0079, 0x006F, 0x0075, 0x0020, 0x006C, 0x0069, 0x006B, 0x0065 | |
:L4_DATA DAT 0x006D, 0x0079, 0x0020, 0x0043, 0x0020, 0x0063, 0x006F, 0x006D, 0x0070, 0x0069, 0x006C, 0x0065, 0x0072, 0x003F, 0x0020, 0x003A, 0x0044 | |
:ENDOFPROGRAM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment