summaryrefslogtreecommitdiff
path: root/interpreter.c
blob: 331da4313d42775ff8889db4aa0a364905c3ac07 (plain)
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
//  interpreter.c
//  Useful for carrying out tests of the CPU instructions.

/*  interpreter.c manual

This program contains many functions which make it easy to write tests for the 6502 CPU.

The expected input is machine code written in plaintext.

The program accepts stdin, so it can be used in two ways.
    - Typing instructions inline.
    - Piping a file in with cat.
I didn't implement arguments because this is only a debug program.

Here are some valid ways to write an LDA with immediate addressing:
    a901
    A901
    A9 01

The interpreter comes with multiple statements for the purpose of debugging the emulator.
    Qq  Quits the program.
        Should be put at the end of a textfile, or the program will segfault (inconsequentially).
    Rr  Resets the virtual computer.
    Pp  Dumps processor state.
    Mm  Dumps a specified page of memory.
        Takes an argument only of the form XX.
        Be aware that unallocated memory is permitted to be dumped.
    /   Prints until newline.
    #   Ignores until newline.
*/


#include"headers/include.h"
#include"headers/debug.h"

//Write a custom getc function here which ignores spaces


int main(int argc, char *argv[]){
    AppleOn();
    

    byte c;

    // Interpreter loop
    while(1){

        //  Retrieve first character of a line
        do {
            c = getc(stdin);
        } while (c == '\n' || c == ' ' || c == '\t');

        //  The following are special debug commands.

        //  Quit program
        if (c == 'Q' || c == 'q'){
            break;
        }

        if (c == 'R' || c == 'r'){
            AppleReset(); //need to flesh out this function
            continue;
        }

        //  Dump processor status
        if (c == 'P' || c == 'p'){
            dStatusDump();
            continue;
        }

        //  Dump a page of memory
        if (c == 'M' || c == 'm'){  
            do {
                c = getc(stdin);
            } while(c == ' ' || c == '\n');
            c = dCharToNum(c) << 4;
            c += dCharToNum(getc(stdin));
            dPageDump(c);
            continue;
        }

        //  Print out a statement
        if (c == '/'){  
            do {
                c = getc(stdin);
                printf("%c", c);
            } while(c != '\n');
            continue;
        }

        //  Comment, so ignores until newline.
        if (c == '#'){
            do {
                c = getc(stdin);
            } while(c != '\n');
            continue;
        }

        //  From here on it is expected input will be mostly correct
        c = dCharToNum(c) << 4;
        c += dCharToNum(getc(stdin));
        address x = 0x0000;
        char z;
        for(int i = ((fAddressGetLengthPrempt(c) * 2) - 2); i > 0; i--) {
            do {
                z = getc(stdin);
            } while (z == ' ' || z == '\t');

            //if (z != '\n'){
            x += dCharToNum(z) << ((4 * (i - 1)));
            /*}else{
                i++;
            }*/
        }
        callInstructionTable(c, x);
    }

}