aboutsummaryrefslogtreecommitdiff
path: root/src/utils/assembler.spec.ts
diff options
context:
space:
mode:
authorUri Shaked2020-01-30 22:56:38 +0200
committerUri Shaked2020-01-30 22:56:38 +0200
commita9f485fc74887cc364f1d59a32648677182832f1 (patch)
tree82a049766a33efd9aeef1ecc94b6aa1b5ad21fb2 /src/utils/assembler.spec.ts
parentfeat(twi): partial TWI master implementation #10 (diff)
downloadavr8js-a9f485fc74887cc364f1d59a32648677182832f1.tar.gz
avr8js-a9f485fc74887cc364f1d59a32648677182832f1.tar.bz2
avr8js-a9f485fc74887cc364f1d59a32648677182832f1.zip
feat: add a simple AVR assembler for use in tests
Diffstat (limited to 'src/utils/assembler.spec.ts')
-rw-r--r--src/utils/assembler.spec.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/utils/assembler.spec.ts b/src/utils/assembler.spec.ts
new file mode 100644
index 0000000..29453eb
--- /dev/null
+++ b/src/utils/assembler.spec.ts
@@ -0,0 +1,49 @@
+import { assemble } from './assembler';
+
+function bytes(hex: string) {
+ const result = new Uint8Array(hex.length / 2);
+ for (let i = 0; i < hex.length; i += 2) {
+ result[i / 2] = parseInt(hex.substr(i, 2), 16);
+ }
+ return result;
+}
+
+describe('AVR assembler', () => {
+ it('should assemble ADD instruction', () => {
+ expect(assemble('ADD r16, r11')).toEqual({
+ bytes: bytes('0b0d'),
+ errors: [],
+ lines: [{ byteOffset: 0, bytes: '0d0b', line: 1, text: 'ADD r16, r11' }]
+ });
+ });
+
+ it('should support labels', () => {
+ expect(assemble('loop: JMP loop').bytes).toEqual(bytes('0c940000'));
+ });
+
+ it('should support mutli-line programs', () => {
+ const input = `
+ start:
+ LDI r16, 15
+ EOR r16, r0
+ BREQ start
+ `;
+ expect(assemble(input).bytes).toEqual(bytes('0fe00025e9f3'));
+ });
+
+ it('should successfully assemble an empty program', () => {
+ expect(assemble('')).toEqual({
+ bytes: new Uint8Array(0),
+ errors: [],
+ lines: []
+ });
+ });
+
+ it('should return an empty byte array in case of program error', () => {
+ expect(assemble('LDI r15, 20')).toEqual({
+ bytes: new Uint8Array(0),
+ errors: ['Line 0: Rd out of range: 16<>31'],
+ lines: []
+ });
+ });
+});