1#!/usr/bin/perl
2
3# Z88DK Z80 Macro Assembler
4#
5# Copyright (C) Paulo Custodio, 2011-2020
6# License: The Artistic License 2.0, http://www.perlfoundation.org/artistic_license_2_0
7# Repository: https://github.com/z88dk/z88dk/
8#
9# Test org
10
11use Modern::Perl;
12use Test::More;
13require './t/testlib.pl';
14
15# no ORG defined
16unlink_testfiles();
17z80asm("start: jp start");
18check_bin_file("test.bin", pack("C*", 0xC3, 0x00, 0x00));
19
20# ORG defined
21unlink_testfiles();
22z80asm("org 0x1234 \n start: jp start");
23check_bin_file("test.bin", pack("C*", 0xC3, 0x34, 0x12));
24
25unlink_testfiles();
26z80asm("defc org = 0x1234 \n org org \n start: jp start");
27check_bin_file("test.bin", pack("C*", 0xC3, 0x34, 0x12));
28
29# ORG defined and overridden by command line
30unlink_testfiles();
31z80asm("org 0x1000 \n start: jp start", "-b -r0x1234");
32check_bin_file("test.bin", pack("C*", 0xC3, 0x34, 0x12));
33
34# no ORG
35unlink_testfiles();
36z80asm("org", "", 1, "", <<'ERR');
37Error at file 'test.asm' line 1: syntax error
38ERR
39
40# ORG redefined
41unlink_testfiles();
42z80asm("org 0x1234 \n org 0x5678", "", 1, "", <<'ERR');
43Error at file 'test.asm' line 2: ORG redefined
44ERR
45
46# ORG OK
47unlink_testfiles();
48z80asm("org 0 \n jp ASMPC");
49check_bin_file("test.bin", pack("C*", 0xC3, 0x00, 0x00));
50
51unlink_testfiles();
52z80asm("org 65535 \n defb ASMPC & 0xFF");
53check_bin_file("test.bin", pack("C*", 0xFF));
54
55unlink_testfiles();
56z80asm("org 65535 \n defb ASMPC >> 8");
57check_bin_file("test.bin", pack("C*", 0xFF));
58
59# ORG out of range
60unlink_testfiles();
61z80asm("org -2", "", 1, "", <<'ERR');
62Error at file 'test.asm' line 1: integer '-2' out of range
63ERR
64
65# ORG not constant
66unlink_testfiles();
67z80asm("extern start \n org start", "", 1, "", <<'ERR');
68Error at file 'test.asm' line 2: expected constant expression
69ERR
70
71# -r -- tested in options.t
72
73# BUG_0025 : JR at org 0 with out-of-range jump crashes WriteListFile()
74unlink_testfiles();
75z80asm("jr ASMPC+2-129", "", 1, "", <<'ERR');
76Error at file 'test.asm' line 1: integer '-129' out of range
77ERR
78
79unlink_testfiles();
80z80asm("jr ASMPC+2+128", "", 1, "", <<'ERR');
81Error at file 'test.asm' line 1: integer '128' out of range
82ERR
83
84# -split-bin, ORG -1
85unlink_testfiles();
86z80asm(<<'END');
87	defw ASMPC
88
89	section code
90	defw ASMPC
91
92	section data
93	defw ASMPC
94
95	section bss		; split file here
96	org 0x4000
97	defw ASMPC
98END
99check_bin_file("test.bin", pack("v*", 0, 2, 4));
100ok ! -f "test_code.bin", "test_code.bin";
101ok ! -f "test_data.bin", "test_data.bin";
102check_bin_file("test_bss.bin", pack("v*", 0x4000));
103
104unlink_testfiles();
105z80asm(<<'END', "-b -split-bin");
106	defw ASMPC		; split file here
107
108	section code	; split file here
109	defw ASMPC
110
111	section data	; split file here
112	defw ASMPC
113
114	section bss		; split file here
115	org 0x4000
116	defw ASMPC
117END
118check_bin_file("test_code.bin", 	pack("v*", 2));
119check_bin_file("test_data.bin", 	pack("v*", 4));
120check_bin_file("test_bss.bin", 		pack("v*", 0x4000));
121
122# ORG -1 to split
123unlink_testfiles();
124z80asm(<<'END', "-b");
125	defw ASMPC
126
127	section code
128	defw ASMPC
129
130	section data	; split file here
131	org 0x4000
132	defw ASMPC
133
134	section bss		; split file here
135	org -1
136	defw ASMPC
137END
138check_bin_file("test.bin", pack("v*", 0, 2));
139ok ! -f "test_code.bin", "test_code.bin";
140check_bin_file("test_data.bin", 	pack("v*", 0x4000));
141check_bin_file("test_bss.bin", 		pack("v*", 0x4002));
142
143unlink_testfiles();
144done_testing();
145