aboutsummaryrefslogtreecommitdiff
path: root/pu2/pu2test.c
blob: 8b94cad9b300167f3e2a69f517c3729ff3a91a92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "pu2.out.c"
#include <stdio.h>
#include <string.h>

/* ANSI colors */
#define CLR_GREEN "\x1b[32m"
#define CLR_RED   "\x1b[31m"
#define CLR_YEL   "\x1b[33m"
#define CLR_RST   "\x1b[0m"

/* test counters */
static int total_tests  = 0;
static int passed_tests = 0;

#define static_len(G) (sizeof(G) / sizeof((G)[ 0 ]))

/* helper: load program at pc and set PC (m[7]) */
void load_prog(int pc, int *prog, int len) {
    int i;
    a = b = 0;
    u = 1;
    /* clear memory (keeps tests isolated) */
    for (i = 0; i < static_len(m); ++i) m[ i ] = 0;
    for (i = 0; i < len; ++i) m[ pc + i ] = prog[ i ];
    m[ 7 ] = pc;
}

/* unified expect function (increments counters and prints colored output) */
void expect_int(const char *name, int got, int want) {
    ++total_tests;
    if (got == want) {
        ++passed_tests;
        printf("%sPASS%s: %s == %d\n", CLR_GREEN, CLR_RST, name, want);
    } else {
        printf("%sFAIL%s: %s == %d (want %d)\n", CLR_RED, CLR_RST, name, got, want);
    }
}

int outbuf[ 512 ];

void zero_Y(void) {
    int ii;
    for (ii = 0; ii < (1 << 10); ++ii) Y[ ii ] = 0;
}

void set_outbuf(void) {
    memset(outbuf, 0, sizeof outbuf);
    O = outbuf;
    zero_Y();
}

/* small helper to run a CPU test with a program loaded at 1024 */
void run_cpu_test(int *prog, int len, void (*setup)(void), const char *name, int (*check)(void)) {
    load_prog(1024, prog, len);
    if (setup) setup();
    pu2();
    if (check) {
        /* check should call expect_int internally or return 1 on success */
        check();
    }
    (void) name; /* kept for future extension */
}

/* example setups and checks for clarity */
int check_a_is_42(void) {
    expect_int("a (load imm 42)", a, 42);
    return 0;
}

int main(void) {
    /* CPU tests */
    {
        int prog[] = { 1, 42, 0, 0 }; /* [opcode, imm, halt, dummy] */
        load_prog(1024, prog, sizeof prog / sizeof prog[ 0 ]);
        a = b = v = c = 0;
        pu2();
        expect_int("a (load imm 42)", a, 42);
    }

    {
        int prog[] = { 2, 0, 0, 0 };
        load_prog(1024, prog, 4);
        /* place value at memory index 500 and set b=500 */
        m[ 500 ] = 99;
        b        = 500;
        a        = 0;
        pu2();
        expect_int("a (a = m[b])", a, 99);
    }

    {
        int prog[] = { 3, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 600;
        b = 1234;
        pu2();
        expect_int("m[600] (m[a]=b)", m[ 600 ], 1234);
    }

    {
        int prog[] = { 4, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 7;
        b = 11;
        v = 0;
        pu2();
        expect_int("a (after swap)", a, 11);
        expect_int("b (after swap)", b, 7);
    }

    {
        int prog[] = { 5, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 5;
        b = 8;
        pu2();
        expect_int("a (a += b)", a, 13);
    }

    {
        int prog[] = { 6, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 20;
        b = 3;
        pu2();
        expect_int("a (a -= b)", a, 17);
    }

    {
        int prog[] = { 7, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 5;
        b = 2;
        pu2();
        expect_int("a (5 && 2)", a, 1);
        load_prog(1024, prog, 4);
        a = 0;
        b = 3;
        pu2();
        expect_int("a (0 && 3)", a, 0);
    }

    {
        int prog[] = { 8, 0, 0, 0 };
        load_prog(1024, prog, 4);
        a = 1;
        b = 1;
        pu2();
        expect_int("a (!a)", a, 0);
        load_prog(1024, prog, 4);
        a = 0;
        b = 1;
        pu2();
        expect_int("a (!a)", a, 1);
    }

    // .. TODO: implement test case for ACC instruction ..

    {
        int prog[] = { 1, 9, 4, 0, 3, 0, 0, 0 };
        /* sequence: load imm 9 into a, swap, store b into m[a], halt */
        load_prog(1024, prog, sizeof prog / sizeof prog[ 0 ]);
        a = 2;
        b = 87; /* after load & swap: a=87, b=9 ; then m[87]=9 */
        pu2();
        expect_int("m[87] (from multi-op)", m[ 87 ], 9);
    }

    /* Assembler (as2) tests: use outbuf */
    {
        int *o;

        set_outbuf();
        as2("hlt");
        o = outbuf;
        expect_int("hlt:opcode", o[ 0 ], 0);
        expect_int("hlt:imm", o[ 1 ], 0);

        set_outbuf();
        as2("imm 42");
        o = outbuf;
        expect_int("imm:opcode", o[ 0 ], 1);
        expect_int("imm:imm", o[ 1 ], 42);

        set_outbuf();
        as2("add sub");
        o = outbuf;
        expect_int("add:opcode", o[ 0 ], 5);
        expect_int("add:imm", o[ 1 ], 0);
        expect_int("sub:opcode", o[ 2 ], 6);
        expect_int("sub:imm", o[ 3 ], 0);

        set_outbuf();
        as2("{0 hlt} @0");
        o = outbuf;
        expect_int("macro-hlt:opcode", o[ 0 ], 0);
        expect_int("macro-hlt:imm", o[ 1 ], 0);

        set_outbuf();
        as2("{0 imm 7} @0");
        o = outbuf;
        expect_int("macro-imm:opcode", o[ 0 ], 1);
        expect_int("macro-imm:imm", o[ 1 ], 7);

        set_outbuf();
        as2("{0 imm 3 add} @0");
        o = outbuf;
        expect_int("macro-mix:imm-op", o[ 0 ], 1);
        expect_int("macro-mix:imm-val", o[ 1 ], 3);
        expect_int("macro-mix:add-op", o[ 2 ], 5);
        expect_int("macro-mix:add-imm", o[ 3 ], 0);
    }

    /* Summary */
    printf(
        "\n%sTest summary%s: %d total, %d passed, %d failed\n",
        CLR_YEL,
        CLR_RST,
        total_tests,
        passed_tests,
        total_tests - passed_tests);

    if (passed_tests == total_tests) {
        printf("%sALL TESTS PASSED%s\n", CLR_GREEN, CLR_RST);
        return 0;
    } else {
        printf("%sSOME TESTS FAILED%s\n", CLR_RED, CLR_RST);
        return 1;
    }
}