Exemplo n.º 1
0
void assemble_binop(void) {
	u16 pc = PC++;
	int a, b;
	int op = token;

	/* alias for push x, pop x */
	if (token == tPUSH) {
		op = tSET;
		a = 0x1a; // push
		b = assemble_operand();
	} else if (token == tPOP) {
		op = tSET;
		a = assemble_operand();
		b = 0x18; // pop
	} else if (token == tNOP) {
		// SET 0,0
		op = tSET;
		a = 0x20;
		b = 0x20;
	} else {
		a = assemble_operand();
		expect(tCOMMA);
		b = assemble_operand();
	}

	/* token to opcode */
	op -= (tSET - 1);
	image[pc] = op | (a << 4) | (b << 10);
}
Exemplo n.º 2
0
void assemble_binop(int op) {
	int pc = PC++;
	int a, b;
	a = assemble_operand();
	expect(tCOMMA);
	b = assemble_operand();
	image[pc] = op | (a << 4) | (b << 10);
}
Exemplo n.º 3
0
void assemble(const char *fn) {
	u16 pc, n;
	fin = fopen(fn, "r");
	filename = fn;
	linenumber = 0;
	if (!fin) die("cannot read file");

	for (;;) {
		next();
		switch (token) {
		case tEOF:
			goto done;
		case tCOLON:
			expect(tSTRING);
			set_label(tstring, PC);
			continue;
		case tWORD:
			assemble_imm_or_label();
			continue;
		case tSET: case tADD: case tSUB: case tMUL:
		case tDIV: case tMOD: case tSHL: case tSHR:
		case tAND: case tBOR: case tXOR: case tIFE:
		case tIFN: case tIFG: case tIFB:
			assemble_binop(token - tXXX); 
			continue;
		case tJSR:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | 0x0010;
			continue;
		default:
			die("unexpected: %s", tnames[token]);
		}
	}
done:
	fclose(fin);
}
Exemplo n.º 4
0
void assemble(const char *fn) {
	u16 pc, n;
	fin = fopen(fn, "r");
	filename = fn;
	linenumber = 0;
	if (!fin) die("cannot read file");

	for (;;) {
		next();
again:
		switch (token) {
		case tEOF:
			goto done;
		case tSTRING:
			expect(tCOLON);
			set_label(tstring, PC);
			continue;
		case tCOLON:
			expect(tSTRING);
			set_label(tstring, PC);
			continue;
		case tWORD: case tDAT: case tDATA: case tDW:
			assemble_imm_or_label();
			goto again;
		case tJMP: // alias for SET PC, ...
			assemble_jump();
			continue;
		case tMOV: // alias for SET
			token = tSET;
		case tSET: case tADD: case tSUB: case tMUL:
		case tDIV: case tMOD: case tSHL: case tSHR:
		case tAND: case tBOR: case tXOR: case tIFE:
		case tIFN: case tIFG: case tIFB:
		case tPUSH: case tPOP: case tNOP:
			assemble_binop();
			continue;
		case tJSR:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x01 << 5);
			continue;
		case tINT:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x08 << 5);
			continue;
		case tIAG:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x09 << 5);
			continue;
		case tIAS:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0a << 5);
			continue;
		case tRFI:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0b << 5);
			continue;
		case tIAQ:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0c << 5);
			continue;
		case tHWN:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x10 << 5);
			continue;
		case tHWQ:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x11 << 5);
			continue;
		case tHWI:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x12 << 5);
			continue;
		default:
			die("unexpected: %s", tnames[token]);
		}
	}
done:
	fclose(fin);
}
Exemplo n.º 5
0
void assemble_jump(void) {
	u16 pc = PC++;
	image[pc] = 0x01c1 | (assemble_operand() << 10);
}