summary refs log tree commit diff
path: root/65c02.ts
blob: 157e66c93f56c832ae91fcf01f453c9ef51da707 (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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import opcodeMatrix from "./opcode_matrix.json" with { type: "json" };

export class BitField {
    bits: boolean[];
    flip(bit: number) {
        this.bits[bit] = !this.bits[bit];
    }
    bit(bit: number) {
        return this.bits[bit]
    }
    setBit(bit: number, value: boolean) {
        this.bits[bit] = value;
    }
    set(value: number) {
        for (let bit = 0; bit < this.bits.length; bit++) {
            const mask: number = 1 << bit;
            this.setBit(bit, (value & mask) != 0)
        }
    }
    num(): number {
        let number = 0;
        for (let bit = 0; bit < this.bits.length; bit++) {
            const mask: number = 1 << bit;
            if (this.bits[bit])
                number |= mask;
        }
        return number;
    }
    constructor (len: number) {
        this.bits = new Array(len).fill(false);
    }
}

export class Register<T=number> extends BitField {
    get value(): number {
        return this.num()
    }
    set value(value: number) {
        this.set(value)
    }
    increment(): number {
        this.set(this.num() + 1);
        return this.num()
    }
    decrement(): number {
        this.set(this.num() - 1);
        return this.num()
    }
    constructor(bits: number) {
        super(bits);
    }
}

export class Pin {
    high: boolean = false;
    HI() {
        this.high = true
    }
    LO() {
        this.high = false
    }
    constructor(init = false) {
        this.high = init
    }
}

export class IO {
    private _data:            BitField = new BitField(8);
    private _address:         BitField = new BitField(16);
    private _busEnable:            Pin = new Pin();
    private _NMI:                  Pin = new Pin();
    private _ready:                Pin = new Pin(true);
    private _reset:                Pin = new Pin(true);
    private _readWrite:            Pin = new Pin();
    private _interruptRequest:     Pin = new Pin();
    get data()             {return this._data}
    get address()          {return this._address}
    get busEnable()        {return this._busEnable}
    get NMI()              {return this._NMI}
    get ready()            {return this._ready}
    get reset()            {return this._reset}
    get readWrite()        {return this._readWrite}
    get interruptRequest() {return this._interruptRequest}
}

/**
 * the shittiest 65c02 emulator ever
 * 
 * not clock cycle accurate because fuck you
 */
export default class The65c02 {
    io:                    IO = new IO();
    //SECTION - register
    programCounter:      Register<16> = new Register(16);
    private registerA:    Register<8> = new Register(8);
    private registerX:    Register<8> = new Register(8);
    private registerY:    Register<8> = new Register(8);

    get regA () { return this.registerA }
    get regX () { return this.registerX }
    get regY () { return this.registerY }

    stackPointer:    Register<8>  = new Register(8);
    status:          Register<8>  = new Register(8);
    //!SECTION
    //SECTION - status reg bits
    get carry(): boolean {return this.status.bit(0)}
    set carry(value:boolean) {this.status.setBit(0, value)}
    get zero(): boolean {return this.status.bit(1)}
    set zero(value:boolean) {this.status.setBit(1, value)}
    get IRQBDisable(): boolean {return this.status.bit(2)}
    set IRQBDisable(value:boolean) {this.status.setBit(2, value)}
    get decimalMode(): boolean {return this.status.bit(3)}
    set decimalMode(value:boolean) {this.status.setBit(3, value)}
    get BRK(): boolean {return this.status.bit(4)}
    set BRK(value:boolean) {this.status.setBit(4, value)}
    // ...1... //
    get overflow(): boolean {return this.status.bit(6)}
    set overflow(value:boolean) {this.status.setBit(6, value)}
    get negative(): boolean {return this.status.bit(7)}
    set negative(value:boolean) {this.status.setBit(7, value)}
    //!SECTION
    read: () => void;
    write: () => void;
    readPC(): BitField {
        this.io.address.set(this.programCounter.num());
        this.read()
        return this.io.data;
    }
    flagZN(num: number) {
        this.negative = (num & 0x80) != 0;
        this.zero = num == 0;
    }
    flagZCN(num: number) {
        this.carry = num > 0xFF
        this.negative = (num & 0x80) != 0;
        this.zero = num == 0;
    }
    instructions: Record<string, (mode: string) => void> = {};
    cycle() {
        if(!this.io.reset.high) {
            // reset register thingies
            this.IRQBDisable = true;
            this.decimalMode = false;
            this.BRK = true;
            // get reset vector
            let resetVector = 0;
            this.io.address.set(0xFFFC);
            this.read()
            resetVector |= this.io.data.num();
            this.io.address.set(0xFFFD);
            this.read()
            resetVector |= this.io.data.num() << 8;
            // move PC to RV
            this.programCounter.set(resetVector)
        }
        this.io.address.set(this.programCounter.num());
        this.read();
        const instruction = this.io.data
            .num()
            .toString(16)
            .padStart(2, '0')
            .toLowerCase();
        const opm: Record<string, { mnemonic: string, mode: string }> = opcodeMatrix;
        if (!opm[instruction])
            throw `not found ${instruction}`
        if (!this.instructions[opm[instruction].mnemonic])
            throw `not implement, sowwy (${opm[instruction].mnemonic}.ts not found)`;
        console.debug(this.programCounter.num().toString(16).padStart(4, '0'),opm[instruction].mnemonic, opm[instruction].mode)
        this.instructions[opm[instruction].mnemonic].call(this, opm[instruction].mode);
    }
    //SECTION - utils
    /** push stuff onto stack */
    push(val: number) {
        if (this.stackPointer.num() >= 0xFF)
            throw 'stack overflow (no, not like the website)';
        this.stackPointer.increment();
        this.io.address.set(0x01FF - this.stackPointer.num());
        this.io.data.set(val);
        this.write();
    }
    pop(): number {
        if (this.stackPointer.num() <= 0)
            throw 'stack underflow';
        this.io.address.set(0x01FF - this.stackPointer.num());
        this.read()
        this.stackPointer.decrement();
        return this.io.data.num()
    }
    getZPAddr(): number {
        this.programCounter.increment()
        const zp = this.readPC().num()
        this.programCounter.increment()
        return zp
    }
    getZPXAddr(): number {
        this.programCounter.increment()
        const zp = this.readPC().num()
        this.programCounter.increment()
        return (this.regX.num() + zp) & 0xFF
    }
    getAbsoluteAddr(): number {
        this.programCounter.increment()
        const lo_abit = this.readPC().num()
        this.programCounter.increment()
        const hi_abit = this.readPC().num()
        return (hi_abit << 8) | lo_abit
    }
    getAbsoluteXAddr(): number {
        this.programCounter.increment()
        const lo_abit = this.readPC().num()
        this.programCounter.increment()
        const hi_abit = this.readPC().num()
        return this.regX.num() + ((hi_abit << 8) | lo_abit)
    }
    getAbsoluteYAddr(): number {
        this.programCounter.increment()
        const lo_abit = this.readPC().num()
        this.programCounter.increment()
        const hi_abit = this.readPC().num()
        return this.regX.num() + ((hi_abit << 8) | lo_abit)
    }
    getIndirectXAddr(): number {
        this.programCounter.increment()
        const addr = this.readPC().num()
        this.io.address.set((this.regX.num() + addr) & 0xFF)
        const lo_abit = this.readPC().num()
        this.io.address.set((this.regX.num() + addr + 1) & 0xFF)
        const hi_abit = this.readPC().num()
        this.programCounter.increment()
        return ((hi_abit << 8) | lo_abit)
    }
    getIndirectYAddr(): number {
        this.programCounter.increment()
        const addr = this.readPC().num()
        this.io.address.set((addr) & 0xFF)
        const lo_abit = this.readPC().num()
        this.io.address.set((addr + 1) & 0xFF)
        const hi_abit = this.readPC().num()
        this.programCounter.increment()
        return ((hi_abit << 8) | lo_abit) + this.regY.num();
    }
    getZPI(): number {
        this.programCounter.increment()
        const addr = this.readPC().num()
        this.io.address.set((addr) & 0xFF)
        const lo_abit = this.readPC().num()
        this.io.address.set((addr + 1) & 0xFF)
        const hi_abit = this.readPC().num()
        this.programCounter.increment()
        return ((hi_abit << 8) | lo_abit);
    }
    getAbsIndexedIndirect(): number {
        this.programCounter.increment()
        let addr = this.readPC().num()
        this.programCounter.increment()
        addr |= this.readPC().num() << 8;
        addr += this.regX.num();
        addr &= 0xFFFF
        this.io.address.set((addr) & 0xFF)
        const lo_abit = this.readPC().num()
        this.io.address.set((addr + 1) & 0xFF)
        const hi_abit = this.readPC().num()
        this.programCounter.increment()
        return ((hi_abit << 8) | lo_abit);
    }
    getAddr(mode: string, allow?: string[]): number {
        if (allow && !allow.includes(mode))
            throw 'disallowed mode'
        switch (mode) {
            case 'immediate':
                this.programCounter.increment()
                this.programCounter.increment()
                return this.programCounter.num() - 1
            case 'zero-page indirect':
                return this.getZPI()
            case 'absolute indexed indirect':
                return this.getAbsIndexedIndirect();
            // deno-lint-ignore no-case-declarations
            case 'relative':
                this.programCounter.increment()
                const offset = this.readPC()
                this.programCounter.increment()
                return this.programCounter.num() + offset.num()
            case 'zero-page':
                return this.getZPAddr()
            case 'zero-page, X-indexed':
                return this.getZPXAddr()
            case 'absolute':
                return this.getAbsoluteAddr()
            case 'absolute, X-indexed':
                return this.getAbsoluteXAddr()
            case 'absolute, Y-indexed':
                return this.getAbsoluteYAddr()
            case 'indirect, Y-indexed':
                return this.getIndirectYAddr()
            case 'X-indexed, indirect':
                return this.getIndirectXAddr()
        
            default:
                throw 'unknown mode '+mode
        }
    }
    //!SECTION
    constructor (read: () => void, write: () => void) {
        this.read = read;
        this.write = write;
    }
    async loadInstructions() {
        const dir = Deno.readDirSync('instructions')
        for (const entry of dir) {
            this.instructions[entry.name.replace('.ts', '')]
                = (await import(`./instructions/${entry.name}`)).default
        }
    }
}