LibDCPU16
Classes | Enumerations | Functions | Variables
dcpu.c File Reference
#include "dcpu.h"
#include "interrupt_queue.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Classes

struct  dcpu
 

Enumerations

enum  dcpu_fsm_state { DCPU_FSM_DECODE_INSTRUCTION, DCPU_FSM_DECODE_OPERAND_B, DCPU_FSM_PERFORM_OPERATION, DCPU_FSM_SKIP_OPERATION }
 

Functions

DCPU dcpu_create (void)
 
void dcpu_destroy (DCPU dcpu)
 
void dcpu_reset (DCPU dcpu)
 
void dcpu_power_on (DCPU dcpu)
 
void dcpu_power_off (DCPU dcpu)
 
void dcpu_flash (DCPU dcpu, char *bytes, size_t length)
 
HARDWARE dcpu_attach (DCPU dcpu)
 
static uint16_t * operand_address (DCPU dcpu, uint16_t operand, BOOLINT is_operand_a)
 
static void dcpu_push (DCPU dcpu, uint16_t value)
 
static uint16_t dcpu_pop (DCPU dcpu)
 
static void dcpu_operation_cost (DCPU dcpu)
 
static void decode_instruction (DCPU dcpu)
 
void dcpu_cycle (DCPU dcpu)
 
void dcpu_process (DCPU dcpu, size_t cycles)
 
void dcpu_interrupt (DCPU dcpu, uint16_t message)
 
void dcpu_set_register (DCPU dcpu, enum Register r, uint16_t value)
 
uint16_t dcpu_get_register (DCPU dcpu, enum Register r)
 
uint16_t * dcpu_memory (DCPU dcpu)
 
void dcpu_tick (DCPU dcpu)
 
void dcpu_on_error (dcpu_error_handler error_handler)
 
size_t dcpu_queue_size (DCPU dcpu)
 

Variables

static dcpu_error_handler DCPU_ERROR_HANDLER = NULL
 
static const size_t operand_cost []
 
static const size_t basic_opcode_cost []
 
static const size_t special_opcode_cost []
 
static uint16_t lem1802_palette_default [16]
 
static uint16_t lem1802_font_default [256]
 

Enumeration Type Documentation

Enumerator
DCPU_FSM_DECODE_INSTRUCTION 
DCPU_FSM_DECODE_OPERAND_B 
DCPU_FSM_PERFORM_OPERATION 
DCPU_FSM_SKIP_OPERATION 

Function Documentation

HARDWARE dcpu_attach ( DCPU  dcpu)

Attempts to reserve a hardware slot on the dcpu. A DCPU can have up to DCPU_HARDWARE_CAPACITY pieces of hardware attached to it. The DCPU is not hot swappable. Attaching hardware to the DCPU while it is powered on will cause the DCPU to catch on fire.

Parameters
dcpuThe DCPU instance.
Returns
A hardware handle if available, NULL if all slots are taken.
DCPU dcpu_create ( void  )

Allocates and initializes a DCPU.

Returns
The allocated and initialized DCPU on success, NULL otherwise.
void dcpu_cycle ( DCPU  dcpu)

Simulate one clock cycle. The DCPU must be powered on and not on fire for this to have an effect.

Parameters
dcpuThe DCPU instance.
void dcpu_destroy ( DCPU  dcpu)

Destructor for the DCPU.

Parameters
dcpuThe DCPU instance.
void dcpu_flash ( DCPU  dcpu,
char *  bytes,
size_t  length 
)

Write some bytes to DCPU RAM. If the DCPU is powered on it will catch fire. If attempting to copy more than DCPU_MEMORY_SIZE bytes the DCPU will catch fire. DCPU_MEMORY_SIZE bytes will be successfully copied into RAM however.

Parameters
dcpuThe DCPU instance.
bytesThe bytes to write.
lengthThe number of bytes to write.
uint16_t dcpu_get_register ( DCPU  dcpu,
enum Register  r 
)

Read a value from a register. The DCPU must be powered on and not on fire for this to have an effect.

Parameters
dcpuThe DCPU instance.
rThe register to read from.
Returns
The value of the register.
void dcpu_interrupt ( DCPU  dcpu,
uint16_t  message 
)

Trigger an interrupt. The DCPU must be powered on and not on fire for this to have an effect. If interrupt queueing is enabled the message will be added to the interrupt_queue to be processed later, otherwise it will be processed after the current instruction completes.

Parameters
dcpuThe DCPU instance.
messageThe message to send the DCPU.
uint16_t* dcpu_memory ( DCPU  dcpu)

Get a pointer to the DCPU memory.

Parameters
dcpuThe DCPU instance.
Returns
A pointer to the DCPU memory, DCPU_MEMORY_SIZE bytes.
void dcpu_on_error ( dcpu_error_handler  error_handler)

Set the error handler callback for all DCPU instances.

Parameters
error_handlerThe error handler.
static void dcpu_operation_cost ( DCPU  dcpu)
static
static uint16_t dcpu_pop ( DCPU  dcpu)
static
void dcpu_power_off ( DCPU  dcpu)

Set a DCPU to be powered off.

Parameters
dcpuThe DCPU instance.
void dcpu_power_on ( DCPU  dcpu)

Set a DCPU to be powered on.

Parameters
dcpuThe DCPU instance.
void dcpu_process ( DCPU  dcpu,
size_t  cycles 
)

Simulate the passed in number of clock cycles. The DCPU must be powered on and not on fire for this to have an effect.

Parameters
dcpuThe DCPU instance.
cyclesThe number of cycles to simulate.
static void dcpu_push ( DCPU  dcpu,
uint16_t  value 
)
static
size_t dcpu_queue_size ( DCPU  dcpu)
void dcpu_reset ( DCPU  dcpu)

Reset the DCPU to a freshly initialized state.

Parameters
dcpuThe DCPU instance.
void dcpu_set_register ( DCPU  dcpu,
enum Register  r,
uint16_t  value 
)

Write a value to a register. The DCPU must be powered on and not on fire for this to have an effect.

Parameters
dcpuThe DCPU instance.
rThe register to write to.
valueThe value write.
void dcpu_tick ( DCPU  dcpu)

Tick all hardware attached to the DCPU.

Parameters
dcpuThe DCPU instance.
static void decode_instruction ( DCPU  dcpu)
static
static uint16_t* operand_address ( DCPU  dcpu,
uint16_t  operand,
BOOLINT  is_operand_a 
)
static

Variable Documentation

const size_t basic_opcode_cost[]
static

A lookup table for the cost in cycles to perform a basic opcode.

dcpu_error_handler DCPU_ERROR_HANDLER = NULL
static
uint16_t lem1802_font_default[256]
static

The default LEM1802 font

uint16_t lem1802_palette_default[16]
static
Initial value:
= {
0x0000,
0x000a,
0x00a0,
0x00aa,
0x0a00,
0x0a0a,
0x0a50,
0x0aaa,
0x0555,
0x055f,
0x05f5,
0x05ff,
0x0f55,
0x0f5f,
0x0ff5,
0x0fff
}

The default LEM1802 palette

const size_t operand_cost[]
static
Initial value:
= {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
0,
0,
1,
0,
0,
0,
1,
1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}

A lookup table for the cost in cycles to process an operand.

const size_t special_opcode_cost[]
static

A lookup table for the cost in cycles to perform an advanced opcode.