LibDCPU16
Macros | Typedefs | Enumerations | Functions
dcpu.h File Reference
#include "bool.h"
#include "hardware.h"
#include "register.h"
#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define DLLEXPORT   __declspec(dllexport)
 
#define DCPU_HARDWARE_CAPACITY   16
 
#define DCPU_MEMORY_SIZE   131072
 

Typedefs

typedef struct dcpuDCPU
 
typedef enum dcpu_error DCPU_ERROR
 
typedef void(* dcpu_error_handler) (DCPU dcpu, DCPU_ERROR error)
 

Enumerations

enum  dcpu_error { DCPU_ERROR_NULL_HANDLE, DCPU_ERROR_INVALID_OPCODE }
 

Functions

DLLEXPORT DCPU dcpu_create (void)
 
DLLEXPORT void dcpu_destroy (DCPU dcpu)
 
DLLEXPORT void dcpu_reset (DCPU dcpu)
 
DLLEXPORT void dcpu_power_on (DCPU dcpu)
 
DLLEXPORT void dcpu_power_off (DCPU dcpu)
 
DLLEXPORT void dcpu_flash (DCPU dcpu, char *bytes, size_t length)
 
DLLEXPORT HARDWARE dcpu_attach (DCPU dcpu)
 
DLLEXPORT void dcpu_cycle (DCPU dcpu)
 
DLLEXPORT void dcpu_process (DCPU dcpu, size_t cycles)
 
DLLEXPORT void dcpu_interrupt (DCPU dcpu, uint16_t message)
 
DLLEXPORT void dcpu_set_register (DCPU dcpu, enum Register r, uint16_t value)
 
DLLEXPORT uint16_t dcpu_get_register (DCPU dcpu, enum Register r)
 
DLLEXPORT uint16_t * dcpu_memory (DCPU dcpu)
 
DLLEXPORT void dcpu_tick (DCPU dcpu)
 
DLLEXPORT void dcpu_on_error (dcpu_error_handler error_handler)
 
size_t dcpu_queue_size (DCPU dcpu)
 

Macro Definition Documentation

#define DCPU_HARDWARE_CAPACITY   16

The maximum number of hardware devices that can be attached.

#define DCPU_MEMORY_SIZE   131072

The size of RAM in bytes.

#define DLLEXPORT   __declspec(dllexport)

Typedef Documentation

typedef struct dcpu* DCPU

An opaque handle to a DCPU.

typedef enum dcpu_error DCPU_ERROR
typedef void(* dcpu_error_handler) (DCPU dcpu, DCPU_ERROR error)

The error handler will be called whenever the dcpu encounters an error.

Parameters
dcpuThe DCPU instance.
errorThe error condition encountered.

Enumeration Type Documentation

enum dcpu_error
Enumerator
DCPU_ERROR_NULL_HANDLE 
DCPU_ERROR_INVALID_OPCODE 

Function Documentation

DLLEXPORT 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.
DLLEXPORT DCPU dcpu_create ( void  )

Allocates and initializes a DCPU.

Returns
The allocated and initialized DCPU on success, NULL otherwise.
DLLEXPORT 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.
DLLEXPORT void dcpu_destroy ( DCPU  dcpu)

Destructor for the DCPU.

Parameters
dcpuThe DCPU instance.
DLLEXPORT 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.
DLLEXPORT 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.
DLLEXPORT 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.
DLLEXPORT 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.
DLLEXPORT void dcpu_on_error ( dcpu_error_handler  error_handler)

Set the error handler callback for all DCPU instances.

Parameters
error_handlerThe error handler.
DLLEXPORT void dcpu_power_off ( DCPU  dcpu)

Set a DCPU to be powered off.

Parameters
dcpuThe DCPU instance.
DLLEXPORT void dcpu_power_on ( DCPU  dcpu)

Set a DCPU to be powered on.

Parameters
dcpuThe DCPU instance.
DLLEXPORT 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.
size_t dcpu_queue_size ( DCPU  dcpu)
DLLEXPORT void dcpu_reset ( DCPU  dcpu)

Reset the DCPU to a freshly initialized state.

Parameters
dcpuThe DCPU instance.
DLLEXPORT 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.
DLLEXPORT void dcpu_tick ( DCPU  dcpu)

Tick all hardware attached to the DCPU.

Parameters
dcpuThe DCPU instance.