Files
DoubleTroubleRecomp/runtime/machine.c
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

138 lines
4.7 KiB
C

/* Matériel : ports VGA, PIT, PIC, clavier ; interruptions matérielles (horloge, clavier). */
#include <stdio.h>
#include "cpu.h"
#include "rt.h"
uint8_t vga_dac[768];
static uint32_t dac_w, dac_r;
uint32_t pit_reload = 0; /* 0 = 65536 (18,2 Hz) */
static int pit_lohi;
uint8_t kbd_port60;
static uint32_t retrace_toggle;
uint32_t rt_in(uint32_t port, int size) {
(void)size;
switch (port) {
case 0x60: return kbd_port60;
case 0x61: return 0;
case 0x64: return 0x1c;
case 0x3c9: { uint32_t v = vga_dac[dac_r % 768]; dac_r++; return v; }
case 0x3da: retrace_toggle ^= 9; if (retrace_toggle & 8) rt_poll(); return retrace_toggle;
case 0x21: case 0xa1: return 0;
default:
if (rt_cfg.verbose) fprintf(stderr, "[port] in %03x\n", port);
return 0xff;
}
}
void rt_out(uint32_t port, uint32_t v, int size) {
(void)size;
switch (port) {
case 0x3c8: dac_w = (v & 0xff) * 3; break;
case 0x3c7: dac_r = (v & 0xff) * 3; break;
case 0x3c9: vga_dac[dac_w % 768] = (uint8_t)(v & 63); dac_w = (dac_w + 1) % 768; break;
case 0x43: pit_lohi = 0; break;
case 0x40:
if (!pit_lohi) { pit_reload = (pit_reload & 0xff00u) | (v & 0xff); pit_lohi = 1; }
else { pit_reload = (pit_reload & 0x00ffu) | ((v & 0xff) << 8); pit_lohi = 0;
if (rt_cfg.verbose) fprintf(stderr, "[pit] diviseur %u\n", pit_reload); }
break;
case 0x20: case 0xa0: case 0x21: case 0xa1: case 0x61: break;
default:
if (rt_cfg.verbose) fprintf(stderr, "[port] out %03x <- %02x\n", port, v);
break;
}
}
/* ------------------------------------------------------------------ interruptions matérielles */
static int in_irq;
void rt_raise_irq(int irq) {
int vec = irq < 8 ? 8 + irq : 0x70 + irq - 8;
uint32_t off = pm_vec_off[vec];
if (!off || in_irq) {
if (irq == 0) WR32(0x46c, RD32(0x46c) + 1);
return;
}
{
uint32_t op = cpu.lf_op, sz = cpu.lf_sz, a = cpu.lf_a, b = cpu.lf_b, r = cpu.lf_r, c = cpu.lf_c, fl = cpu.lf_flags;
uint32_t df = cpu.df;
in_irq = 1;
PUSH32(get_eflags());
PUSH32(pm_vec_sel[vec]);
PUSH32(0xfffffff0u); /* adresse de retour fictive : iretd la dépile */
rt_call(off);
in_irq = 0;
cpu.lf_op = op; cpu.lf_sz = sz; cpu.lf_a = a; cpu.lf_b = b; cpu.lf_r = r; cpu.lf_c = c; cpu.lf_flags = fl;
cpu.df = df;
}
}
void rt_callback(uint32_t addr) {
Cpu save = cpu;
in_irq = 1;
PUSH32(0xfffffff0u);
rt_call(addr);
in_irq = 0;
cpu = save;
}
/* minuteur logiciel qui remplace AIL_register_timer */
static uint32_t game_timer_fn, game_timer_hz;
static uint64_t next_game_us;
void rt_game_timer(uint32_t callback, uint32_t hz) {
game_timer_fn = callback; game_timer_hz = hz; next_game_us = 0;
}
/* appelé régulièrement par le code traduit (POLL) */
uint32_t poll_counter;
static uint64_t next_tick_us, next_frame_us;
static uint32_t polls_since_frame;
void rt_poll(void) {
uint64_t now;
int guard = 0;
poll_counter = 0;
if (in_irq) return;
now = host_time_us();
if (!next_tick_us) { next_tick_us = now; next_frame_us = now; }
while (now >= next_tick_us && guard++ < 8) {
uint32_t div = pit_reload ? pit_reload : 65536u;
next_tick_us += (uint64_t)div * 1000000u / 1193182u;
rt_raise_irq(0);
}
if (guard >= 8) next_tick_us = now;
if (game_timer_fn) {
if (!next_game_us) next_game_us = now;
guard = 0;
while (now >= next_game_us && guard++ < 8) {
next_game_us += 1000000u / game_timer_hz;
rt_callback(game_timer_fn);
}
if (guard >= 8) next_game_us = now;
}
/* Le jeu attend en boucle active (compteur de ticks, retour de balayage) : passé un certain
* nombre de passages sans nouvelle image à produire, on dort un peu au lieu de brûler un cœur. */
if (++polls_since_frame > 4096 && now + 2000 < next_frame_us) host_idle();
if (now >= next_frame_us) {
uint8_t sc;
polls_since_frame = 0;
next_frame_us = now + 14286; /* ~70 Hz */
host_pump();
if (host_quit) rt_exit(0);
audio_update();
host_frame();
if (pm_vec_off[9]) /* gestionnaire clavier du jeu : IRQ1 ; sinon file BIOS (int 16h) */
while (host_key_pop(&sc)) { kbd_port60 = sc; rt_raise_irq(1); }
}
}
/* état matériel d'un démarrage (avant de lancer un autre programme) */
void machine_reset(void) {
pit_reload = 0; pit_lohi = 0; kbd_port60 = 0; retrace_toggle = 0;
in_irq = 0;
game_timer_fn = 0; game_timer_hz = 0; next_game_us = 0;
next_tick_us = 0; next_frame_us = 0; polls_since_frame = 0; poll_counter = 0;
}