Skip to content

Commit 42f0b39

Browse files
committed
Apply AssemblyScript fixes to instruction.ts
1 parent dbd93f8 commit 42f0b39

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

assembly/v2/cpu/instruction.ts

+43-34
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
* the datasheet)
99
*
1010
* Copyright (C) 2019, 2020 Uri Shaked
11+
*
12+
* v0.18.6 - Modified by Dario Götze
1113
*/
1214

1315
import { CPU } from './cpu';
14-
import { u16 } from '../types';
1516

16-
function isTwoWordInstruction(opcode: u16) {
17+
// DG Added boolean return type
18+
function isTwoWordInstruction(opcode: u16): boolean {
1719
return (
1820
/* LDS */
1921
(opcode & 0xfe0f) === 0x9000 ||
@@ -26,8 +28,15 @@ function isTwoWordInstruction(opcode: u16) {
2628
);
2729
}
2830

29-
export function avrInstruction(cpu: CPU) {
30-
const opcode = cpu.progMem[cpu.pc];
31+
/*
32+
DG
33+
- Added void return type
34+
- Replaced 'const { pc22Bits } = cpu' with 'const pc22Bits = cpu.pc22Bits'
35+
- Set type of opcode to u32 to fix implicit type cast problems
36+
- Apply explicit typecasts using function expression for ts portability. See https://www.assemblyscript.org/portability.html
37+
*/
38+
export function avrInstruction(cpu: CPU): void {
39+
const opcode : u32 = cpu.progMem[cpu.pc];
3140

3241
if ((opcode & 0xfc00) === 0x1c00) {
3342
/* ADC, 0001 11rd dddd rrrr */
@@ -63,7 +72,7 @@ export function avrInstruction(cpu: CPU) {
6372
const addr = 2 * ((opcode & 0x30) >> 4) + 24;
6473
const value = cpu.dataView.getUint16(addr, true);
6574
const R = (value + ((opcode & 0xf) | ((opcode & 0xc0) >> 2))) & 0xffff;
66-
cpu.dataView.setUint16(addr, R, true);
75+
cpu.dataView.setUint16(addr, u16(R), true);
6776
let sreg = cpu.data[95] & 0xe0;
6877
sreg |= R ? 0 : 2;
6978
sreg |= 0x8000 & R ? 4 : 0;
@@ -104,38 +113,38 @@ export function avrInstruction(cpu: CPU) {
104113
cpu.data[95] = sreg;
105114
} else if ((opcode & 0xff8f) === 0x9488) {
106115
/* BCLR, 1001 0100 1sss 1000 */
107-
cpu.data[95] &= ~(1 << ((opcode & 0x70) >> 4));
116+
cpu.data[95] &= ~(1 << u8((opcode & 0x70) >> 4));
108117
} else if ((opcode & 0xfe08) === 0xf800) {
109118
/* BLD, 1111 100d dddd 0bbb */
110119
const b = opcode & 7;
111120
const d = (opcode & 0x1f0) >> 4;
112-
cpu.data[d] = (~(1 << b) & cpu.data[d]) | (((cpu.data[95] >> 6) & 1) << b);
121+
cpu.data[d] = (~(1 << b) & cpu.data[d]) | (((cpu.data[95] >> 6) & 1) << u8(b));
113122
} else if ((opcode & 0xfc00) === 0xf400) {
114123
/* BRBC, 1111 01kk kkkk ksss */
115-
if (!(cpu.data[95] & (1 << (opcode & 7)))) {
124+
if (!(cpu.data[95] & (1 << u8(opcode & 7)))) {
116125
cpu.pc = cpu.pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
117126
cpu.cycles++;
118127
}
119128
} else if ((opcode & 0xfc00) === 0xf000) {
120129
/* BRBS, 1111 00kk kkkk ksss */
121-
if (cpu.data[95] & (1 << (opcode & 7))) {
130+
if (cpu.data[95] & (1 << u8(opcode & 7))) {
122131
cpu.pc = cpu.pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
123132
cpu.cycles++;
124133
}
125134
} else if ((opcode & 0xff8f) === 0x9408) {
126135
/* BSET, 1001 0100 0sss 1000 */
127-
cpu.data[95] |= 1 << ((opcode & 0x70) >> 4);
136+
cpu.data[95] |= 1 << u8((opcode & 0x70) >> 4);
128137
} else if ((opcode & 0xfe08) === 0xfa00) {
129138
/* BST, 1111 101d dddd 0bbb */
130139
const d = cpu.data[(opcode & 0x1f0) >> 4];
131140
const b = opcode & 7;
132-
cpu.data[95] = (cpu.data[95] & 0xbf) | ((d >> b) & 1 ? 0x40 : 0);
141+
cpu.data[95] = (cpu.data[95] & 0xbf) | ((d >> u8(b)) & 1 ? 0x40 : 0);
133142
} else if ((opcode & 0xfe0e) === 0x940e) {
134143
/* CALL, 1001 010k kkkk 111k kkkk kkkk kkkk kkkk */
135144
const k = cpu.progMem[cpu.pc + 1] | ((opcode & 1) << 16) | ((opcode & 0x1f0) << 13);
136145
const ret = cpu.pc + 2;
137146
const sp = cpu.dataView.getUint16(93, true);
138-
const { pc22Bits } = cpu;
147+
const pc22Bits = cpu.pc22Bits;
139148
cpu.data[sp] = 255 & ret;
140149
cpu.data[sp - 1] = (ret >> 8) & 255;
141150
if (pc22Bits) {
@@ -146,10 +155,10 @@ export function avrInstruction(cpu: CPU) {
146155
cpu.cycles += pc22Bits ? 4 : 3;
147156
} else if ((opcode & 0xff00) === 0x9800) {
148157
/* CBI, 1001 1000 AAAA Abbb */
149-
const A = opcode & 0xf8;
150-
const b = opcode & 7;
158+
const A = u16(opcode) & 0xf8;
159+
const b = u8(opcode) & 7;
151160
const R = cpu.readData((A >> 3) + 32);
152-
const mask = 1 << b;
161+
const mask : u8 = 1 << b;
153162
cpu.writeData((A >> 3) + 32, R & ~mask, mask);
154163
} else if ((opcode & 0xfe0f) === 0x9400) {
155164
/* COM, 1001 010d dddd 0000 */
@@ -292,7 +301,7 @@ export function avrInstruction(cpu: CPU) {
292301
/* ICALL, 1001 0101 0000 1001 */
293302
const retAddr = cpu.pc + 1;
294303
const sp = cpu.dataView.getUint16(93, true);
295-
const { pc22Bits } = cpu;
304+
const pc22Bits = cpu.pc22Bits;
296305
cpu.data[sp] = retAddr & 255;
297306
cpu.data[sp - 1] = (retAddr >> 8) & 255;
298307
if (pc22Bits) {
@@ -307,7 +316,7 @@ export function avrInstruction(cpu: CPU) {
307316
cpu.cycles++;
308317
} else if ((opcode & 0xf800) === 0xb000) {
309318
/* IN, 1011 0AAd dddd AAAA */
310-
const i = cpu.readData(((opcode & 0xf) | ((opcode & 0x600) >> 5)) + 32);
319+
const i = cpu.readData(u16((opcode & 0xf) | ((opcode & 0x600) >> 5)) + 32);
311320
cpu.data[(opcode & 0x1f0) >> 4] = i;
312321
} else if ((opcode & 0xfe0f) === 0x9403) {
313322
/* INC, 1001 010d dddd 0011 */
@@ -393,7 +402,7 @@ export function avrInstruction(cpu: CPU) {
393402
cpu.cycles++;
394403
cpu.data[(opcode & 0x1f0) >> 4] = cpu.readData(
395404
cpu.dataView.getUint16(28, true) +
396-
((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8))
405+
u16((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8))
397406
);
398407
} else if ((opcode & 0xfe0f) === 0x8000) {
399408
/* LDZ, 1000 000d dddd 0000 */
@@ -419,7 +428,7 @@ export function avrInstruction(cpu: CPU) {
419428
cpu.cycles++;
420429
cpu.data[(opcode & 0x1f0) >> 4] = cpu.readData(
421430
cpu.dataView.getUint16(30, true) +
422-
((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8))
431+
u16((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8))
423432
);
424433
} else if (opcode === 0x95c8) {
425434
/* LPM, 1001 0101 1100 1000 */
@@ -511,7 +520,7 @@ export function avrInstruction(cpu: CPU) {
511520
cpu.data[95] = sreg;
512521
} else if ((opcode & 0xf800) === 0xb800) {
513522
/* OUT, 1011 1AAr rrrr AAAA */
514-
cpu.writeData(((opcode & 0xf) | ((opcode & 0x600) >> 5)) + 32, cpu.data[(opcode & 0x1f0) >> 4]);
523+
cpu.writeData(u16((opcode & 0xf) | ((opcode & 0x600) >> 5)) + 32, cpu.data[(opcode & 0x1f0) >> 4]);
515524
} else if ((opcode & 0xfe0f) === 0x900f) {
516525
/* POP, 1001 000d dddd 1111 */
517526
const value = cpu.dataView.getUint16(93, true) + 1;
@@ -529,7 +538,7 @@ export function avrInstruction(cpu: CPU) {
529538
const k = (opcode & 0x7ff) - (opcode & 0x800 ? 0x800 : 0);
530539
const retAddr = cpu.pc + 1;
531540
const sp = cpu.dataView.getUint16(93, true);
532-
const { pc22Bits } = cpu;
541+
const pc22Bits = cpu.pc22Bits;
533542
cpu.data[sp] = 255 & retAddr;
534543
cpu.data[sp - 1] = (retAddr >> 8) & 255;
535544
if (pc22Bits) {
@@ -540,7 +549,7 @@ export function avrInstruction(cpu: CPU) {
540549
cpu.cycles += pc22Bits ? 3 : 2;
541550
} else if (opcode === 0x9508) {
542551
/* RET, 1001 0101 0000 1000 */
543-
const { pc22Bits } = cpu;
552+
const pc22Bits = cpu.pc22Bits;
544553
const i = cpu.dataView.getUint16(93, true) + (pc22Bits ? 3 : 2);
545554
cpu.dataView.setUint16(93, i, true);
546555
cpu.pc = (cpu.data[i - 1] << 8) + cpu.data[i] - 1;
@@ -550,7 +559,7 @@ export function avrInstruction(cpu: CPU) {
550559
cpu.cycles += pc22Bits ? 4 : 3;
551560
} else if (opcode === 0x9518) {
552561
/* RETI, 1001 0101 0001 1000 */
553-
const { pc22Bits } = cpu;
562+
const pc22Bits = cpu.pc22Bits;
554563
const i = cpu.dataView.getUint16(93, true) + (pc22Bits ? 3 : 2);
555564
cpu.dataView.setUint16(93, i, true);
556565
cpu.pc = (cpu.data[i - 1] << 8) + cpu.data[i] - 1;
@@ -603,23 +612,23 @@ export function avrInstruction(cpu: CPU) {
603612
cpu.data[95] = sreg;
604613
} else if ((opcode & 0xff00) === 0x9a00) {
605614
/* SBI, 1001 1010 AAAA Abbb */
606-
const target = ((opcode & 0xf8) >> 3) + 32;
607-
const mask = 1 << (opcode & 7);
615+
const target = u16((opcode & 0xf8) >> 3) + 32;
616+
const mask : u8 = 1 << u8(opcode & 7);
608617
cpu.writeData(target, cpu.readData(target) | mask, mask);
609618
cpu.cycles++;
610619
} else if ((opcode & 0xff00) === 0x9900) {
611620
/* SBIC, 1001 1001 AAAA Abbb */
612-
const value = cpu.readData(((opcode & 0xf8) >> 3) + 32);
613-
if (!(value & (1 << (opcode & 7)))) {
621+
const value = cpu.readData(u16((opcode & 0xf8) >> 3) + 32);
622+
if (!(value & (1 << u8(opcode & 7)))) {
614623
const nextOpcode = cpu.progMem[cpu.pc + 1];
615624
const skipSize = isTwoWordInstruction(nextOpcode) ? 2 : 1;
616625
cpu.cycles += skipSize;
617626
cpu.pc += skipSize;
618627
}
619628
} else if ((opcode & 0xff00) === 0x9b00) {
620629
/* SBIS, 1001 1011 AAAA Abbb */
621-
const value = cpu.readData(((opcode & 0xf8) >> 3) + 32);
622-
if (value & (1 << (opcode & 7))) {
630+
const value = cpu.readData(u16((opcode & 0xf8) >> 3) + 32);
631+
if (value & (1 << u8(opcode & 7))) {
623632
const nextOpcode = cpu.progMem[cpu.pc + 1];
624633
const skipSize = isTwoWordInstruction(nextOpcode) ? 2 : 1;
625634
cpu.cycles += skipSize;
@@ -629,7 +638,7 @@ export function avrInstruction(cpu: CPU) {
629638
/* SBIW, 1001 0111 KKdd KKKK */
630639
const i = 2 * ((opcode & 0x30) >> 4) + 24;
631640
const a = cpu.dataView.getUint16(i, true);
632-
const l = (opcode & 0xf) | ((opcode & 0xc0) >> 2);
641+
const l = u16((opcode & 0xf) | ((opcode & 0xc0) >> 2));
633642
const R = a - l;
634643
cpu.dataView.setUint16(i, R, true);
635644
let sreg = cpu.data[95] & 0xc0;
@@ -643,15 +652,15 @@ export function avrInstruction(cpu: CPU) {
643652
cpu.cycles++;
644653
} else if ((opcode & 0xfe08) === 0xfc00) {
645654
/* SBRC, 1111 110r rrrr 0bbb */
646-
if (!(cpu.data[(opcode & 0x1f0) >> 4] & (1 << (opcode & 7)))) {
655+
if (!(cpu.data[(opcode & 0x1f0) >> 4] & (1 << u8(opcode & 7)))) {
647656
const nextOpcode = cpu.progMem[cpu.pc + 1];
648657
const skipSize = isTwoWordInstruction(nextOpcode) ? 2 : 1;
649658
cpu.cycles += skipSize;
650659
cpu.pc += skipSize;
651660
}
652661
} else if ((opcode & 0xfe08) === 0xfe00) {
653662
/* SBRS, 1111 111r rrrr 0bbb */
654-
if (cpu.data[(opcode & 0x1f0) >> 4] & (1 << (opcode & 7))) {
663+
if (cpu.data[(opcode & 0x1f0) >> 4] & (1 << u8(opcode & 7))) {
655664
const nextOpcode = cpu.progMem[cpu.pc + 1];
656665
const skipSize = isTwoWordInstruction(nextOpcode) ? 2 : 1;
657666
cpu.cycles += skipSize;
@@ -715,7 +724,7 @@ export function avrInstruction(cpu: CPU) {
715724
/* STDY, 10q0 qq1r rrrr 1qqq */
716725
cpu.writeData(
717726
cpu.dataView.getUint16(28, true) +
718-
((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8)),
727+
u16((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8)),
719728
cpu.data[(opcode & 0x1f0) >> 4]
720729
);
721730
cpu.cycles++;
@@ -743,7 +752,7 @@ export function avrInstruction(cpu: CPU) {
743752
/* STDZ, 10q0 qq1r rrrr 0qqq */
744753
cpu.writeData(
745754
cpu.dataView.getUint16(30, true) +
746-
((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8)),
755+
u16((opcode & 7) | ((opcode & 0xc00) >> 7) | ((opcode & 0x2000) >> 8)),
747756
cpu.data[(opcode & 0x1f0) >> 4]
748757
);
749758
cpu.cycles++;

0 commit comments

Comments
 (0)