pico9918-core 1.3.0
TMS9918A / F18A video display processor emulation in C99
Loading...
Searching...
No Matches
tms9900.c File Reference

pico9918-core - TMS9900 CPU interpreter (portable C) More...

#include "tms9900.h"
#include <stddef.h>
+ Include dependency graph for tms9900.c:

Go to the source code of this file.

Data Structures

struct  Operand
 

Macros

#define GPU_WINDOW_READS   0x05FFu
 
#define GPU_WINDOW_WRITES   0x097Fu
 
#define watch_write(cpu, addr)   ((void)0)
 

Functions

static void set_flags_word (Tms9900Cpu *cpu, uint16_t v)
 
static void set_flags_byte (Tms9900Cpu *cpu, uint8_t v)
 
static uint32_t gpu_addr (uint16_t a)
 
static uint8_t rd8 (Tms9900Cpu *cpu, uint16_t a)
 
static void wr8 (Tms9900Cpu *cpu, uint16_t a, uint8_t v)
 
static uint16_t rd16 (Tms9900Cpu *cpu, uint16_t a)
 
static void wr16 (Tms9900Cpu *cpu, uint16_t a, uint16_t v)
 
static uint32_t wp_addr (Tms9900Cpu *cpu, uint8_t r)
 
static uint16_t get_reg (Tms9900Cpu *cpu, uint8_t r)
 
static void set_reg (Tms9900Cpu *cpu, uint8_t r, uint16_t v)
 
static uint16_t fetchw (Tms9900Cpu *cpu)
 
static Operand decode_operand (Tms9900Cpu *cpu, uint8_t field, uint8_t is_byte)
 
static void store_operand (Tms9900Cpu *cpu, const Operand *o, uint16_t v)
 
static uint16_t add16 (Tms9900Cpu *cpu, uint16_t a, uint16_t b)
 
static uint16_t sub16 (Tms9900Cpu *cpu, uint16_t a, uint16_t b)
 
static uint8_t add8 (Tms9900Cpu *cpu, uint8_t a, uint8_t b)
 
static uint8_t sub8 (Tms9900Cpu *cpu, uint8_t a, uint8_t b)
 
static void cmp16 (Tms9900Cpu *cpu, uint16_t first, uint16_t second)
 
static void cmp8 (Tms9900Cpu *cpu, uint8_t src, uint8_t dst)
 
static uint16_t slx16 (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static uint16_t sra16 (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static uint16_t srl16 (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static uint16_t src16 (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static uint16_t slc16 (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static uint16_t src_through_c (Tms9900Cpu *cpu, uint16_t v, uint8_t count)
 
static void handle_two_operand (Tms9900Cpu *cpu, uint16_t inst)
 
static void handle_format9 (Tms9900Cpu *cpu, uint16_t inst)
 
static int handle_branch_group (Tms9900Cpu *cpu, uint16_t inst)
 
static void handle_shift_rotate (Tms9900Cpu *cpu, uint16_t inst)
 
static void handle_f18a_stack (Tms9900Cpu *cpu, uint16_t inst)
 
static int handle_immediate_system (Tms9900Cpu *cpu, uint16_t inst)
 
static void handle_jump_single (Tms9900Cpu *cpu, uint16_t inst)
 
static void handle_cru_single_bit (void)
 
void tms9900_init (Tms9900Cpu *cpu, uint8_t *mem, uint8_t *regx38, uint16_t pc, uint16_t wp)
 
uint16_t run9900_c (Tms9900Cpu *cpu)
 
uint16_t run9900_budget_c (Tms9900Cpu *cpu, uint32_t budget, bool *outOfBudget)
 

Variables

static const uint8_t parity_tbl [256]
 
static const uint16_t gpu_window_base [16]
 
static const uint16_t gpu_window_mask [16]
 

Detailed Description

pico9918-core - TMS9900 CPU interpreter (portable C)

Copyright (c) 2026 Troy Schrapel

This code is licensed under the MIT license

https://github.com/visrealm/pico9918-core

This is a full reimplementation of JasonACT's RP2040 thumb assembly core (thumb9900_m0.S / thumb9900_m33.S) intended for non-ARM targets. It aims to be functionally identical where it matters to the GPU: the status flag layout matches the assembly core (LGT=0x80, AGT=0x40, EQ=0x20, C=0x10, OV=0x08, P=0x04). CRU (LDCR, STCR, SBO, SBZ, TB) and CKON/CKOF/LREX are no-ops in both cores.

Memory layout follows the existing GPU glue: a 64 KiB byte array that stores TMS9900 words in big-endian order. The workspace pointer (WP) is a byte address into that array and register access uses big-endian word loads/stores.

The interpreter stops when bit0 of the control byte at regx38 is cleared, or when an IDLE instruction is executed, returning the current PC just like the assembly core. Behavior of auto-increment and index modes matches the original core (increments are 1 for byte ops, 2 for word ops).

Definition in file tms9900.c.

Macro Definition Documentation

◆ GPU_WINDOW_READS

#define GPU_WINDOW_READS   0x05FFu

Definition at line 117 of file tms9900.c.

◆ GPU_WINDOW_WRITES

#define GPU_WINDOW_WRITES   0x097Fu

Definition at line 118 of file tms9900.c.

◆ watch_write

#define watch_write (   cpu,
  addr 
)    ((void)0)

Definition at line 167 of file tms9900.c.

Function Documentation

◆ set_flags_word()

static void set_flags_word ( Tms9900Cpu cpu,
uint16_t  v 
)
inlinestatic

Definition at line 51 of file tms9900.c.

◆ set_flags_byte()

static void set_flags_byte ( Tms9900Cpu cpu,
uint8_t  v 
)
inlinestatic

Definition at line 70 of file tms9900.c.

◆ gpu_addr()

static uint32_t gpu_addr ( uint16_t  a)
inlinestatic

Definition at line 120 of file tms9900.c.

◆ rd8()

static uint8_t rd8 ( Tms9900Cpu cpu,
uint16_t  a 
)
inlinestatic

Definition at line 126 of file tms9900.c.

◆ wr8()

static void wr8 ( Tms9900Cpu cpu,
uint16_t  a,
uint8_t  v 
)
inlinestatic

Definition at line 133 of file tms9900.c.

◆ rd16()

static uint16_t rd16 ( Tms9900Cpu cpu,
uint16_t  a 
)
inlinestatic

Definition at line 147 of file tms9900.c.

◆ wr16()

static void wr16 ( Tms9900Cpu cpu,
uint16_t  a,
uint16_t  v 
)
inlinestatic

Definition at line 152 of file tms9900.c.

◆ wp_addr()

static uint32_t wp_addr ( Tms9900Cpu cpu,
uint8_t  r 
)
inlinestatic

Definition at line 172 of file tms9900.c.

◆ get_reg()

static uint16_t get_reg ( Tms9900Cpu cpu,
uint8_t  r 
)
inlinestatic

Definition at line 177 of file tms9900.c.

◆ set_reg()

static void set_reg ( Tms9900Cpu cpu,
uint8_t  r,
uint16_t  v 
)
inlinestatic

Definition at line 183 of file tms9900.c.

◆ fetchw()

static uint16_t fetchw ( Tms9900Cpu cpu)
inlinestatic

Definition at line 200 of file tms9900.c.

◆ decode_operand()

static Operand decode_operand ( Tms9900Cpu cpu,
uint8_t  field,
uint8_t  is_byte 
)
static

Definition at line 207 of file tms9900.c.

◆ store_operand()

static void store_operand ( Tms9900Cpu cpu,
const Operand o,
uint16_t  v 
)
static

Definition at line 251 of file tms9900.c.

◆ add16()

static uint16_t add16 ( Tms9900Cpu cpu,
uint16_t  a,
uint16_t  b 
)
inlinestatic

Definition at line 279 of file tms9900.c.

◆ sub16()

static uint16_t sub16 ( Tms9900Cpu cpu,
uint16_t  a,
uint16_t  b 
)
inlinestatic

Definition at line 292 of file tms9900.c.

◆ add8()

static uint8_t add8 ( Tms9900Cpu cpu,
uint8_t  a,
uint8_t  b 
)
inlinestatic

Definition at line 307 of file tms9900.c.

◆ sub8()

static uint8_t sub8 ( Tms9900Cpu cpu,
uint8_t  a,
uint8_t  b 
)
inlinestatic

Definition at line 318 of file tms9900.c.

◆ cmp16()

static void cmp16 ( Tms9900Cpu cpu,
uint16_t  first,
uint16_t  second 
)
inlinestatic

Definition at line 335 of file tms9900.c.

◆ cmp8()

static void cmp8 ( Tms9900Cpu cpu,
uint8_t  src,
uint8_t  dst 
)
inlinestatic

Definition at line 348 of file tms9900.c.

◆ slx16()

static uint16_t slx16 ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 361 of file tms9900.c.

◆ sra16()

static uint16_t sra16 ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 382 of file tms9900.c.

◆ srl16()

static uint16_t srl16 ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 400 of file tms9900.c.

◆ src16()

static uint16_t src16 ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 418 of file tms9900.c.

◆ slc16()

static uint16_t slc16 ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 433 of file tms9900.c.

◆ src_through_c()

static uint16_t src_through_c ( Tms9900Cpu cpu,
uint16_t  v,
uint8_t  count 
)
inlinestatic

Definition at line 450 of file tms9900.c.

◆ handle_two_operand()

static void handle_two_operand ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 1031 of file tms9900.c.

◆ handle_format9()

static void handle_format9 ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 834 of file tms9900.c.

◆ handle_branch_group()

static int handle_branch_group ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 737 of file tms9900.c.

◆ handle_shift_rotate()

static void handle_shift_rotate ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 808 of file tms9900.c.

◆ handle_f18a_stack()

static void handle_f18a_stack ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 978 of file tms9900.c.

◆ handle_immediate_system()

static int handle_immediate_system ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 478 of file tms9900.c.

◆ handle_jump_single()

static void handle_jump_single ( Tms9900Cpu cpu,
uint16_t  inst 
)
inlinestatic

Definition at line 556 of file tms9900.c.

◆ handle_cru_single_bit()

static void handle_cru_single_bit ( void  )
inlinestatic

Definition at line 802 of file tms9900.c.

◆ tms9900_init()

void tms9900_init ( Tms9900Cpu cpu,
uint8_t *  mem,
uint8_t *  regx38,
uint16_t  pc,
uint16_t  wp 
)

Definition at line 1127 of file tms9900.c.

◆ run9900_c()

uint16_t run9900_c ( Tms9900Cpu cpu)

Definition at line 1140 of file tms9900.c.

◆ run9900_budget_c()

uint16_t run9900_budget_c ( Tms9900Cpu cpu,
uint32_t  budget,
bool *  outOfBudget 
)

Definition at line 1145 of file tms9900.c.

Variable Documentation

◆ parity_tbl

const uint8_t parity_tbl[256]
static
Initial value:
= {
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4, 0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0}

Definition at line 38 of file tms9900.c.

◆ gpu_window_base

const uint16_t gpu_window_base[16]
static
Initial value:
= {0x0000, 0x0000, 0x0000, 0x0000, 0x4000, 0x5000, 0x6000, 0x7000,
0x8000, 0x0000, 0xB00E, 0xB002, 0x0000, 0x0000, 0x0000, 0x0000}

Definition at line 110 of file tms9900.c.

◆ gpu_window_mask

const uint16_t gpu_window_mask[16]
static
Initial value:
= {0x3FFF, 0x3FFF, 0x3FFF, 0x3FFF, 0x07FF, 0x007F, 0x003F, 0x0001,
0x000F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}

Definition at line 113 of file tms9900.c.