Files
DoubleTroubleRecomp/runtime/cpu.h
T
linkatplugandClaude Opus 5.5 dd2a7efd8d Premier commit : recompilation statique de Bud Tucker in Double Trouble
- tools/ : lecteur LE (DOS/4GW) et traducteur x86 -> C (Capstone)
- runtime/ : CPU, FPU x87, DOS, VGA/PIT, mixeur et emulation AIL,
  remplacements de fonctions, hotes SDL2 et headless
- build.bat : generation et compilation MSVC 2022
- README, feuille de route (docs/ROADMAP.md), .gitignore excluant
  tout fichier du jeu et tout code genere

Etat du code au moment du commit : travail en cours sur le son
des cinematiques (runtime/ail.c).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 02:18:30 +01:00

184 lines
8.9 KiB
C

/* État du processeur x86 émulé et primitives utilisées par le code traduit (build/gen/*.c).
*
* Mémoire : un seul tableau M, adressé par les adresses linéaires du jeu (modèle plat de
* DOS/4GW : code 0x10000, données 0x40000, écran VGA 0xA0000, tas au-delà de 1 Mo).
* Indicateurs : paresseux. Chaque instruction qui les modifie note (opération, taille, a, b,
* résultat) ; on ne calcule CF, ZF, etc. que si quelqu'un les lit.
*/
#pragma once
#include <stdint.h>
#include <string.h>
typedef union {
uint32_t d;
uint16_t w;
struct { uint8_t l, h; } b;
} Reg;
enum { LF_ADD, LF_ADC, LF_SUB, LF_SBB, LF_LOGIC, LF_INC, LF_DEC, LF_NEG, LF_FIXED };
enum { S_ES, S_CS, S_SS, S_DS, S_FS, S_GS };
typedef struct {
Reg r[8]; /* eax ecx edx ebx esp ebp esi edi */
uint32_t lf_op, lf_sz, lf_a, lf_b, lf_r, lf_c, lf_flags;
uint32_t df;
uint16_t seg[6];
uint32_t sb[6]; /* bases des segments (0 en modèle plat) */
} Cpu;
typedef void (*RecompFn)(void);
typedef struct { uint32_t addr; RecompFn fn; } FnEntry;
extern Cpu cpu;
extern uint8_t *M;
extern const FnEntry *cur_fn_table; /* table du programme en cours (jeu ou intro) */
extern unsigned cur_fn_count;
#define MEM_SIZE (64u << 20)
#define MEM_MASK (MEM_SIZE - 1u)
#define R(n) cpu.r[n].d
#define W(n) cpu.r[n].w
#define BL_(n) cpu.r[n].b.l
#define BH_(n) cpu.r[n].b.h
#define SEGR(n) cpu.seg[n]
#define SB(n) cpu.sb[n]
/* ------------------------------------------------------------------ mémoire */
static inline uint8_t RD8(uint32_t a) { return M[a & MEM_MASK]; }
static inline uint16_t RD16(uint32_t a) { uint16_t v; memcpy(&v, M + (a & MEM_MASK), 2); return v; }
static inline uint32_t RD32(uint32_t a) { uint32_t v; memcpy(&v, M + (a & MEM_MASK), 4); return v; }
static inline void WR8(uint32_t a, uint32_t v) { M[a & MEM_MASK] = (uint8_t)v; }
static inline void WR16(uint32_t a, uint32_t v) { uint16_t x = (uint16_t)v; memcpy(M + (a & MEM_MASK), &x, 2); }
static inline void WR32(uint32_t a, uint32_t v) { memcpy(M + (a & MEM_MASK), &v, 4); }
static inline void PUSH32(uint32_t v) { R(4) -= 4; WR32(SB(S_SS) + R(4), v); }
static inline uint32_t POP32(void) { uint32_t v = RD32(SB(S_SS) + R(4)); R(4) += 4; return v; }
static inline void PUSH16(uint32_t v) { R(4) -= 2; WR16(SB(S_SS) + R(4), v); }
static inline uint16_t POP16(void) { uint16_t v = RD16(SB(S_SS) + R(4)); R(4) += 2; return v; }
void PUSHAD(void);
void POPAD(void);
/* ------------------------------------------------------------------ indicateurs */
#define LF(op, sz, a, b, r, c) (cpu.lf_op = (op), cpu.lf_sz = (sz), cpu.lf_a = (a), cpu.lf_b = (b), \
cpu.lf_r = (r), cpu.lf_c = (c))
static inline uint32_t lf_sign(void) { return 1u << (cpu.lf_sz - 1); }
static inline int cCF(void) {
switch (cpu.lf_op) {
case LF_ADD: return cpu.lf_r < cpu.lf_a;
case LF_ADC: return cpu.lf_c ? cpu.lf_r <= cpu.lf_a : cpu.lf_r < cpu.lf_a;
case LF_SUB: return cpu.lf_a < cpu.lf_b;
case LF_SBB: return cpu.lf_c ? cpu.lf_a <= cpu.lf_b : cpu.lf_a < cpu.lf_b;
case LF_LOGIC: return 0;
case LF_INC: case LF_DEC: return (int)cpu.lf_c;
case LF_NEG: return cpu.lf_a != 0;
default: return (int)(cpu.lf_flags & 1u);
}
}
static inline int cZF(void) { return cpu.lf_op == LF_FIXED ? (int)((cpu.lf_flags >> 6) & 1u) : cpu.lf_r == 0; }
static inline int cSF(void) { return cpu.lf_op == LF_FIXED ? (int)((cpu.lf_flags >> 7) & 1u) : (cpu.lf_r & lf_sign()) != 0; }
static inline int cOF(void) {
uint32_t s = lf_sign();
switch (cpu.lf_op) {
case LF_ADD: case LF_ADC: return ((cpu.lf_a ^ cpu.lf_r) & (cpu.lf_b ^ cpu.lf_r) & s) != 0;
case LF_SUB: case LF_SBB: return ((cpu.lf_a ^ cpu.lf_b) & (cpu.lf_a ^ cpu.lf_r) & s) != 0;
case LF_INC: return cpu.lf_r == s;
case LF_DEC: return cpu.lf_a == s;
case LF_NEG: return cpu.lf_a == s;
case LF_LOGIC: return 0;
default: return (int)((cpu.lf_flags >> 11) & 1u);
}
}
static inline int cPF(void) {
uint32_t v;
if (cpu.lf_op == LF_FIXED) return (int)((cpu.lf_flags >> 2) & 1u);
v = cpu.lf_r & 0xffu;
v ^= v >> 4; v ^= v >> 2; v ^= v >> 1;
return !(v & 1u);
}
static inline int cAF(void) {
switch (cpu.lf_op) {
case LF_ADD: case LF_ADC: case LF_SUB: case LF_SBB: return ((cpu.lf_a ^ cpu.lf_b ^ cpu.lf_r) & 0x10u) != 0;
case LF_INC: return (cpu.lf_r & 0xfu) == 0;
case LF_DEC: return (cpu.lf_r & 0xfu) == 0xfu;
case LF_NEG: return (cpu.lf_a & 0xfu) != 0;
case LF_LOGIC: return 0;
default: return (int)((cpu.lf_flags >> 4) & 1u);
}
}
uint32_t get_eflags(void);
void set_eflags(uint32_t v);
void LF_MULFLAGS(int cf_of);
void LF_ZF(int z);
/* ------------------------------------------------------------------ décalages */
uint32_t SHL8(uint32_t a, uint32_t n); uint32_t SHL16(uint32_t a, uint32_t n); uint32_t SHL32(uint32_t a, uint32_t n);
uint32_t SHR8(uint32_t a, uint32_t n); uint32_t SHR16(uint32_t a, uint32_t n); uint32_t SHR32(uint32_t a, uint32_t n);
uint32_t SAR8(uint32_t a, uint32_t n); uint32_t SAR16(uint32_t a, uint32_t n); uint32_t SAR32(uint32_t a, uint32_t n);
uint32_t ROL8(uint32_t a, uint32_t n); uint32_t ROL16(uint32_t a, uint32_t n); uint32_t ROL32(uint32_t a, uint32_t n);
uint32_t ROR8(uint32_t a, uint32_t n); uint32_t ROR16(uint32_t a, uint32_t n); uint32_t ROR32(uint32_t a, uint32_t n);
uint32_t RCL8(uint32_t a, uint32_t n); uint32_t RCL16(uint32_t a, uint32_t n); uint32_t RCL32(uint32_t a, uint32_t n);
uint32_t RCR8(uint32_t a, uint32_t n); uint32_t RCR16(uint32_t a, uint32_t n); uint32_t RCR32(uint32_t a, uint32_t n);
/* ------------------------------------------------------------------ divisions */
void DIV8(uint32_t v, uint32_t at); void DIV16(uint32_t v, uint32_t at); void DIV32(uint32_t v, uint32_t at);
void IDIV8(uint32_t v, uint32_t at); void IDIV16(uint32_t v, uint32_t at); void IDIV32(uint32_t v, uint32_t at);
/* ------------------------------------------------------------------ chaînes */
#define STR_DECL(op) void op##8(void); void op##16(void); void op##32(void); \
void REP_##op##8(void); void REP_##op##16(void); void REP_##op##32(void);
STR_DECL(MOVS) STR_DECL(STOS) STR_DECL(LODS)
void CMPS8(void); void CMPS16(void); void CMPS32(void);
void SCAS8(void); void SCAS16(void); void SCAS32(void);
void REPE_CMPS8(void); void REPE_CMPS16(void); void REPE_CMPS32(void);
void REPNE_CMPS8(void); void REPNE_CMPS16(void); void REPNE_CMPS32(void);
void REPE_SCAS8(void); void REPE_SCAS16(void); void REPE_SCAS32(void);
void REPNE_SCAS8(void); void REPNE_SCAS16(void); void REPNE_SCAS32(void);
/* ------------------------------------------------------------------ segments, flot */
void set_seg(int s, uint16_t sel);
void rt_call(uint32_t addr);
#define CALL_INDIRECT(t) rt_call(t)
#define JUMP_INDIRECT(t) rt_call(t)
void JUMP_FAR(uint32_t ea); /* jmp far [m16:32] : chaînage vers un ancien gestionnaire */
void CALL_FAR(uint32_t ea, uint32_t ret); /* call far [m16:32] */
void TRAP(uint32_t addr);
extern uint32_t poll_counter;
void rt_poll(void);
#define POLL_EVERY 256u
#define POLL() do { if (++poll_counter >= POLL_EVERY) rt_poll(); } while (0)
/* ------------------------------------------------------------------ DOS, BIOS, ports */
void rt_int(uint32_t n);
uint32_t rt_in(uint32_t port, int size);
void rt_out(uint32_t port, uint32_t v, int size);
/* ------------------------------------------------------------------ FPU */
typedef struct { int k; uint32_t a; } FpuOp; /* k : 16/32/64/80 mémoire, 100+i st(i), 200 ax */
#define MEM16(a) ((FpuOp){16, (a)})
#define MEM32(a) ((FpuOp){32, (a)})
#define MEM64(a) ((FpuOp){64, (a)})
#define MEM80(a) ((FpuOp){80, (a)})
#define ST_REG(i) ((FpuOp){100 + (i), 0})
#define REG_ax ((FpuOp){200, 0})
void FPU_FLD(FpuOp o); void FPU_FILD(FpuOp o); void FPU_FST(FpuOp o); void FPU_FSTP(FpuOp o);
void FPU_FIST(FpuOp o); void FPU_FISTP(FpuOp o);
void FPU_FLDZ(void); void FPU_FLD1(void); void FPU_FLDLG2(void);
void FPU_FADD(FpuOp o); void FPU_FSUB(FpuOp o); void FPU_FSUBR(FpuOp o); void FPU_FMUL(FpuOp o);
void FPU_FDIV(FpuOp o); void FPU_FDIVR(FpuOp o);
void FPU_FIADD(FpuOp o); void FPU_FISUB(FpuOp o); void FPU_FIMUL(FpuOp o); void FPU_FIDIV(FpuOp o);
void FPU_FDIVP(FpuOp o); void FPU_FMULP(FpuOp o); void FPU_FADDP(FpuOp o); void FPU_FSUBP(FpuOp o);
void FPU_FCOMP(FpuOp o); void FPU_FCOM(FpuOp o); void FPU_FCOMPP(void);
void FPU_FCHS(void); void FPU_FABS(void); void FPU_FRNDINT(void); void FPU_FYL2X(void);
void FPU_FXCH(FpuOp a, FpuOp b);
void FPU_FNSTSW(FpuOp o); void FPU_FNSTCW(FpuOp o); void FPU_FLDCW(FpuOp o);
void FPU_FNINIT(void); void FPU_FNSAVE(FpuOp o); void FPU_FRSTOR(FpuOp o);
/* formes à deux opérandes st(i), st(j) */
void FPU_FADD2(FpuOp a, FpuOp b); void FPU_FSUB2(FpuOp a, FpuOp b); void FPU_FSUBR2(FpuOp a, FpuOp b);
void FPU_FMUL2(FpuOp a, FpuOp b); void FPU_FDIV2(FpuOp a, FpuOp b); void FPU_FDIVR2(FpuOp a, FpuOp b);
void FPU_FSUBRP(FpuOp o); void FPU_FDIVRP(FpuOp o);