2019-09-28 21:09:51 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
uint8_t mem[64*1024];
|
|
|
|
|
|
|
|
union regset {
|
2019-10-02 21:14:18 +02:00
|
|
|
uint8_t r8[8]; // AL, AH, CL, CH...
|
|
|
|
uint16_t r16[8]; // AX, CX, DX, BX, SP, BP, SI, DI
|
2019-09-28 21:09:51 +02:00
|
|
|
} regset;
|
|
|
|
|
2019-10-02 21:14:18 +02:00
|
|
|
typedef struct flagbits {
|
|
|
|
unsigned int c : 1;
|
|
|
|
unsigned int u1 : 1;
|
|
|
|
unsigned int p : 1;
|
|
|
|
unsigned int u3 : 1;
|
|
|
|
|
|
|
|
unsigned int a : 1;
|
|
|
|
unsigned int u5 : 1;
|
|
|
|
unsigned int z : 1;
|
|
|
|
unsigned int s : 1;
|
|
|
|
|
|
|
|
unsigned int t : 1;
|
|
|
|
unsigned int i : 1;
|
|
|
|
unsigned int d : 1;
|
|
|
|
unsigned int o : 1;
|
|
|
|
|
|
|
|
unsigned int u12 : 4;
|
|
|
|
} flagbits;
|
|
|
|
|
|
|
|
union flags {
|
|
|
|
uint16_t reg;
|
|
|
|
struct flagbits bit;
|
|
|
|
} flags;
|
2019-09-29 14:23:59 +02:00
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
uint16_t ip = 0x100;
|
|
|
|
uint16_t scratch;
|
2019-10-05 00:54:37 +02:00
|
|
|
uint16_t prefixes = 0;
|
2019-09-28 21:09:51 +02:00
|
|
|
uint16_t reg;
|
|
|
|
|
|
|
|
void* rmptr;
|
|
|
|
#define RM8 *(uint8_t*)rmptr
|
|
|
|
#define RM16 *(uint16_t*)rmptr
|
|
|
|
|
|
|
|
#define R8(x) regset.r8[fixr8ref(x)]
|
|
|
|
#define R16(x) regset.r16[x]
|
|
|
|
|
|
|
|
#define AL regset.r8[0]
|
2019-10-02 21:14:18 +02:00
|
|
|
#define AH regset.r8[1]
|
2019-09-28 21:09:51 +02:00
|
|
|
#define CL regset.r8[2]
|
|
|
|
#define DL regset.r8[4]
|
2019-09-28 22:38:35 +02:00
|
|
|
#define AX regset.r16[0]
|
2019-10-05 00:54:37 +02:00
|
|
|
#define CX regset.r16[1]
|
2019-09-28 21:09:51 +02:00
|
|
|
#define BX regset.r16[3]
|
|
|
|
#define SP regset.r16[4]
|
|
|
|
#define BP regset.r16[5]
|
|
|
|
#define SI regset.r16[6]
|
|
|
|
#define DI regset.r16[7]
|
|
|
|
|
2019-10-05 00:54:37 +02:00
|
|
|
#define FLAGMASK 0xF02A
|
|
|
|
|
2019-09-29 14:23:59 +02:00
|
|
|
#define ADD 0
|
|
|
|
#define OR 1
|
|
|
|
#define ADC 2
|
|
|
|
#define SBB 3
|
|
|
|
#define AND 4
|
|
|
|
#define SUB 5
|
|
|
|
#define XOR 6
|
|
|
|
#define CMP 7
|
|
|
|
#define TEST 8
|
|
|
|
|
2019-10-05 00:54:37 +02:00
|
|
|
#define PREFIX_REPE 1
|
|
|
|
#define PREFIX_REPNE 2
|
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
const char* const regnames[] = { "AX", "CX", "DX", "BX", "SP", "BP", "SI", "DI", 0 };
|
|
|
|
|
|
|
|
// Dump registers
|
|
|
|
void dump() {
|
|
|
|
printf("\n");
|
|
|
|
int i;
|
|
|
|
for (i=0; i<8; i++) {
|
|
|
|
printf("%s=%04X ", regnames[i], regset.r16[i]);
|
|
|
|
}
|
2019-10-02 21:14:18 +02:00
|
|
|
printf("%c", flags.bit.o ? 'O' : '-');
|
|
|
|
printf("%c", flags.bit.d ? 'D' : '-');
|
|
|
|
printf("%c", flags.bit.i ? 'I' : '-');
|
|
|
|
printf("%c", flags.bit.t ? 'T' : '-');
|
|
|
|
printf("%c", flags.bit.s ? 'S' : '-');
|
|
|
|
printf("%c", flags.bit.z ? 'Z' : '-');
|
|
|
|
printf("%c", flags.bit.a ? 'A' : '-');
|
|
|
|
printf("%c", flags.bit.p ? 'P' : '-');
|
|
|
|
printf("%c", flags.bit.c ? 'C' : '-');
|
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
printf("\n");
|
2019-10-01 23:35:01 +02:00
|
|
|
printf("IP=%04X : %02X %02X\n", ip, mem[ip], mem[ip+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void arith8(int op, int8_t* dst, int src) {
|
|
|
|
// I'll just assume this sign-extends
|
|
|
|
int t = *dst;
|
|
|
|
int p = *dst;
|
|
|
|
switch (op) {
|
|
|
|
case ADD: t += src; break;
|
|
|
|
case OR: t |= src; break;
|
2019-10-02 21:14:18 +02:00
|
|
|
case ADC: t += (src + flags.bit.c); break;
|
|
|
|
case SBB: t -= (src + flags.bit.c); break;
|
2019-10-01 23:35:01 +02:00
|
|
|
case AND: t &= src; break;
|
|
|
|
case SUB: t -= src; break;
|
|
|
|
case XOR: t ^= src; break;
|
|
|
|
case CMP: t -= src; break;
|
|
|
|
case TEST: t &= src; break;
|
|
|
|
}
|
2019-10-02 21:14:18 +02:00
|
|
|
flags.bit.z = !t;
|
|
|
|
flags.bit.c = !!((t ^ p) >> 8);
|
2019-10-01 23:35:01 +02:00
|
|
|
if (op==CMP || op==TEST) return;
|
|
|
|
*dst = t;
|
2019-09-28 21:09:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 14:23:59 +02:00
|
|
|
void arith16(int op, int16_t* dst, int src) {
|
|
|
|
// I'll just assume this sign-extends
|
|
|
|
int t = *dst;
|
|
|
|
int p = *dst;
|
|
|
|
switch (op) {
|
|
|
|
case ADD: t += src; break;
|
|
|
|
case OR: t |= src; break;
|
2019-10-02 21:14:18 +02:00
|
|
|
case ADC: t += (src + flags.bit.c); break;
|
|
|
|
case SBB: t -= (src + flags.bit.c); break;
|
2019-09-29 14:23:59 +02:00
|
|
|
case AND: t &= src; break;
|
|
|
|
case SUB: t -= src; break;
|
|
|
|
case XOR: t ^= src; break;
|
|
|
|
case CMP: t -= src; break;
|
|
|
|
case TEST: t &= src; break;
|
|
|
|
}
|
2019-10-02 21:14:18 +02:00
|
|
|
flags.bit.z = !t;
|
|
|
|
flags.bit.c = !!((t ^ p) >> 8);
|
2019-09-29 14:23:59 +02:00
|
|
|
if (op==CMP || op==TEST) return;
|
|
|
|
*dst = t;
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
int fixr8ref(int r) {
|
|
|
|
return ((r << 1) & 0b110) | ((r >> 2) & 0b1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t imm8() {
|
|
|
|
uint8_t r = *(uint8_t*)&mem[ip];
|
|
|
|
ip++;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t imm16() {
|
|
|
|
uint16_t r = *(uint16_t*)&mem[ip];
|
|
|
|
ip+=2;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BIT8 8
|
|
|
|
#define BIT16 16
|
|
|
|
void modrm(int size) {
|
|
|
|
uint8_t mod = (mem[ip] >> 6) & 0b11; // highest 2 bits
|
|
|
|
reg = (mem[ip] >> 3) & 0b111;
|
|
|
|
uint8_t rm = (mem[ip]) & 0b111;
|
|
|
|
ip++;
|
|
|
|
if (mod == 3) {
|
2019-09-29 14:23:59 +02:00
|
|
|
if (size == BIT16) {
|
2019-09-28 21:09:51 +02:00
|
|
|
rmptr = &R16(rm);
|
|
|
|
} else {
|
|
|
|
rmptr = &R8(rm);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mod == 0 && rm == 6) {
|
|
|
|
rmptr = mem + (int16_t)imm16();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch(rm) {
|
|
|
|
case 0: rmptr = mem + BX + SI; break;
|
|
|
|
case 1: rmptr = mem + BX + DI; break;
|
|
|
|
case 2: rmptr = mem + BP + SI; break;
|
|
|
|
case 3: rmptr = mem + BP + DI; break;
|
|
|
|
case 4: rmptr = mem + SI; break;
|
|
|
|
case 5: rmptr = mem + DI; break;
|
|
|
|
case 6: rmptr = mem + BP; break;
|
|
|
|
case 7: rmptr = mem + BX; break;
|
|
|
|
}
|
|
|
|
if (mod == 1) rmptr += (int8_t)imm8();
|
|
|
|
if (mod == 2) rmptr += (int16_t)imm16();
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(uint16_t v) {
|
|
|
|
SP -= 2;
|
|
|
|
*(uint16_t*)&mem[SP] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t pop() {
|
|
|
|
uint16_t v = *(uint16_t*)&mem[SP];
|
|
|
|
SP += 2;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:14:18 +02:00
|
|
|
void cpm_syscall(int number) {
|
2019-09-28 21:09:51 +02:00
|
|
|
switch(number) {
|
|
|
|
case 0:
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
printf("%c", DL);
|
|
|
|
break;
|
|
|
|
default:
|
2019-10-02 21:14:18 +02:00
|
|
|
fprintf(stderr, "Fatal: Unhandled CP/M syscall %Xh\n", number);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dos_syscall(int number) {
|
|
|
|
switch(number) {
|
|
|
|
case 0:
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Fatal: Unhandled DOS syscall %Xh\n", number);
|
2019-09-28 21:09:51 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_intr(int number) {
|
|
|
|
switch(number) {
|
|
|
|
case 0x20:
|
2019-10-02 21:14:18 +02:00
|
|
|
dos_syscall(0);
|
|
|
|
break;
|
|
|
|
case 0x21:
|
|
|
|
dos_syscall(AH);
|
2019-09-28 21:09:51 +02:00
|
|
|
break;
|
|
|
|
case 0xE0:
|
2019-10-02 21:14:18 +02:00
|
|
|
cpm_syscall(CL);
|
2019-09-28 21:09:51 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Fatal: Unhandled interrupt %Xh\n", number);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute a single instruction
|
|
|
|
void step() {
|
|
|
|
uint8_t opcode = mem[ip];
|
|
|
|
ip++;
|
|
|
|
switch(opcode) {
|
2019-10-01 23:35:01 +02:00
|
|
|
case 0x00: // ARITH r/m8,reg8
|
|
|
|
case 0x08:
|
|
|
|
case 0x10:
|
|
|
|
case 0x18:
|
|
|
|
case 0x20:
|
|
|
|
case 0x28:
|
|
|
|
case 0x30:
|
|
|
|
case 0x38:
|
|
|
|
modrm(BIT8);
|
|
|
|
arith8(opcode >> 3, &RM8, R8(reg));
|
|
|
|
break;
|
|
|
|
case 0x01: // ARITH r/m16,reg16
|
|
|
|
case 0x09:
|
|
|
|
case 0x11:
|
|
|
|
case 0x19:
|
|
|
|
case 0x21:
|
|
|
|
case 0x29:
|
|
|
|
case 0x31:
|
|
|
|
case 0x39:
|
|
|
|
modrm(BIT16);
|
|
|
|
arith16(opcode >> 3, &RM16, R16(reg));
|
|
|
|
break;
|
2019-10-05 00:54:37 +02:00
|
|
|
case 0x40 ... 0x47: // INC reg16
|
2019-10-08 21:54:13 +02:00
|
|
|
reg=opcode-0x40;
|
|
|
|
R16(reg)++;
|
|
|
|
flags.bit.z=!R16(reg);
|
2019-10-05 00:54:37 +02:00
|
|
|
break;
|
|
|
|
case 0x48 ... 0x4F: // DEC reg16
|
2019-10-08 21:54:13 +02:00
|
|
|
reg=opcode-0x48;
|
|
|
|
R16(reg)--;
|
|
|
|
flags.bit.z=!R16(reg);
|
2019-10-05 00:54:37 +02:00
|
|
|
break;
|
2019-09-29 14:23:59 +02:00
|
|
|
case 0x50 ... 0x57: // PUSH reg16
|
|
|
|
push(R16(opcode-0x50));
|
|
|
|
break;
|
|
|
|
case 0x58 ... 0x5F: // POP reg16
|
|
|
|
R16(opcode-0x58)=pop();
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0x70 ... 0x7F: // Jcc
|
|
|
|
reg = ip + (int8_t)imm8();
|
2019-10-03 00:35:48 +02:00
|
|
|
switch((opcode-0x70) >> 1) {
|
|
|
|
case 0: scratch = flags.bit.o; break;
|
|
|
|
case 1: scratch = flags.bit.s; break;
|
|
|
|
case 2: scratch = flags.bit.z; break;
|
|
|
|
case 3: scratch = flags.bit.c; break;
|
|
|
|
case 4: scratch = flags.bit.c || flags.bit.z; break;
|
|
|
|
case 5: scratch = flags.bit.s != flags.bit.o; break;
|
|
|
|
case 6: scratch = flags.bit.z || flags.bit.s != flags.bit.o; break;
|
|
|
|
case 7: scratch = flags.bit.p; break;
|
|
|
|
}
|
|
|
|
if (!!(opcode & 1) != scratch) {
|
2019-09-28 21:09:51 +02:00
|
|
|
ip = reg;
|
|
|
|
}
|
|
|
|
break;
|
2019-09-29 14:23:59 +02:00
|
|
|
case 0x83: // ARITH r/m16,imm8
|
|
|
|
modrm(BIT16);
|
|
|
|
arith16(reg, &RM16, imm8());
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0x84: // TEST r/m8,reg8
|
|
|
|
modrm(BIT8);
|
2019-09-29 14:23:59 +02:00
|
|
|
scratch = R8(reg) & RM8;
|
2019-09-28 21:09:51 +02:00
|
|
|
break;
|
|
|
|
case 0x88: // MOV r/m8,reg8
|
|
|
|
modrm(BIT8);
|
|
|
|
RM8 = R8(reg);
|
|
|
|
break;
|
2019-10-01 23:35:01 +02:00
|
|
|
case 0x89: // MOV r/m16,reg16
|
|
|
|
modrm(BIT16);
|
|
|
|
RM16 = R16(reg);
|
|
|
|
break;
|
2019-09-28 22:38:35 +02:00
|
|
|
case 0x8B: // MOV reg16,r/m16
|
|
|
|
modrm(BIT16);
|
|
|
|
R16(reg) = RM16;
|
|
|
|
break;
|
2019-09-29 14:23:59 +02:00
|
|
|
case 0x9C: // PUSHF
|
2019-10-02 21:14:18 +02:00
|
|
|
push(flags.reg | FLAGMASK);
|
2019-09-29 14:23:59 +02:00
|
|
|
break;
|
2019-09-28 22:38:35 +02:00
|
|
|
case 0xA1: // MOV AX,memoffs16
|
|
|
|
AX = *(uint16_t*)&mem[imm16()];
|
|
|
|
break;
|
2019-10-05 00:54:37 +02:00
|
|
|
case 0xAA: // STOSB
|
|
|
|
scratch = 1;
|
|
|
|
if (prefixes & PREFIX_REPE) {
|
|
|
|
scratch = CX;
|
|
|
|
CX = 0;
|
|
|
|
}
|
|
|
|
for(;scratch>0;scratch--) {
|
|
|
|
mem[DI] = AL;
|
|
|
|
DI += 1 - (flags.bit.d << 1);
|
|
|
|
}
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0xAC: // LODSB
|
2019-10-05 00:54:37 +02:00
|
|
|
scratch = 1;
|
|
|
|
if (prefixes & PREFIX_REPE) {
|
|
|
|
scratch = CX;
|
|
|
|
CX = 0;
|
|
|
|
}
|
|
|
|
for(;scratch>0;scratch--) {
|
|
|
|
AL = mem[SI];
|
|
|
|
SI += 1 - (flags.bit.d << 1);
|
|
|
|
}
|
2019-09-28 21:09:51 +02:00
|
|
|
break;
|
|
|
|
case 0xB0 ... 0xB7: // MOV reg8,imm8
|
|
|
|
reg = opcode-0xB0;
|
|
|
|
R8(reg) = imm8();
|
|
|
|
break;
|
|
|
|
case 0xB8 ... 0xBF: // MOV reg16,imm16
|
|
|
|
reg = opcode-0xB8;
|
|
|
|
R16(reg) = imm16();
|
|
|
|
break;
|
|
|
|
case 0xC3: // RET
|
|
|
|
ip = pop();
|
|
|
|
break;
|
|
|
|
case 0xCC: // INT3
|
|
|
|
dump();
|
|
|
|
break;
|
|
|
|
case 0xCD: // INT imm8
|
|
|
|
handle_intr(imm8());
|
|
|
|
break;
|
|
|
|
case 0xE8: // CALL rw
|
|
|
|
reg = ip + (int16_t)imm16();
|
|
|
|
if (reg == 5) {
|
2019-10-02 21:14:18 +02:00
|
|
|
cpm_syscall(CL);
|
2019-09-28 21:09:51 +02:00
|
|
|
} else {
|
|
|
|
push(ip);
|
2019-09-28 22:38:35 +02:00
|
|
|
ip = reg;
|
2019-09-28 21:09:51 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-10-04 23:54:10 +02:00
|
|
|
case 0xE9:
|
|
|
|
reg = imm16();
|
|
|
|
ip += (int16_t)reg;
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0xEB: // JMP rb
|
|
|
|
ip = ip + (int8_t)imm8();
|
|
|
|
break;
|
2019-10-05 00:54:37 +02:00
|
|
|
case 0xF2:
|
|
|
|
prefixes |= PREFIX_REPNE;
|
|
|
|
return;
|
|
|
|
case 0xF3:
|
|
|
|
prefixes |= PREFIX_REPE;
|
|
|
|
return;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0xFA: // CLI
|
2019-10-05 00:54:37 +02:00
|
|
|
flags.bit.i = 0;
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
case 0xFB: // STI
|
2019-10-05 00:54:37 +02:00
|
|
|
flags.bit.i = 1;
|
|
|
|
break;
|
2019-09-28 21:09:51 +02:00
|
|
|
default:
|
|
|
|
ip--;
|
|
|
|
dump();
|
|
|
|
fprintf(stderr, "Invalid opcode at IP=%04X\n", ip);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-05 00:54:37 +02:00
|
|
|
prefixes = 0;
|
|
|
|
if (flags.bit.t) dump();
|
2019-09-28 21:09:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 22:38:35 +02:00
|
|
|
void copy_cmdline(char* str) {
|
|
|
|
int i, c;
|
|
|
|
uint8_t *len = &mem[0x80];
|
|
|
|
char* ptr = (char*)&mem[0x81];
|
|
|
|
c = strlen(str);
|
|
|
|
// Clip at max length
|
|
|
|
if (c>0x7E) {
|
|
|
|
fprintf(stderr, "Command line too long, max is 126 bytes\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
memcpy(ptr, str, c);
|
|
|
|
ptr[c]=0x0D;
|
|
|
|
*len=c;
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
memset(&mem, sizeof(mem), 0);
|
|
|
|
memset(®set, sizeof(regset), 0);
|
2019-10-02 21:14:18 +02:00
|
|
|
// Prepare default exit into int 20h
|
2019-09-28 21:09:51 +02:00
|
|
|
mem[0]=0xCD;
|
|
|
|
mem[1]=0x20;
|
|
|
|
push(0);
|
|
|
|
|
2019-10-02 21:14:18 +02:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
while(argc && argv[0][0]=='-') {
|
|
|
|
switch(argv[0][1]) {
|
|
|
|
case 't':
|
|
|
|
flags.bit.t = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown option %s\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
FILE* fd = fopen(argv[0], "r");
|
|
|
|
fread(mem + ip, 1, sizeof(mem) - ip, fd);
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "No COM file specified\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
copy_cmdline(argv[0]);
|
2019-09-28 22:38:35 +02:00
|
|
|
} else {
|
|
|
|
copy_cmdline("");
|
|
|
|
}
|
|
|
|
|
2019-09-28 21:09:51 +02:00
|
|
|
while(1) {
|
|
|
|
step();
|
|
|
|
}
|
|
|
|
}
|