Skip to content

Commit

Permalink
removed tic80local struct and done some Studio refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed Feb 21, 2022
1 parent 617e5b5 commit 3dc2fea
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 549 deletions.
17 changes: 9 additions & 8 deletions include/tic80.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ extern "C" {

#define TIC80_KEY_BUFFER 4
#define TIC80_SAMPLERATE 44100
#define TIC80_SAMPLETYPE s16
#define TIC80_SAMPLESIZE sizeof(TIC80_SAMPLETYPE)
#define TIC80_SAMPLE_CHANNELS 2
#define TIC80_FRAMERATE 60

typedef enum {
Expand All @@ -57,18 +60,16 @@ typedef struct
{
void (*trace)(const char* text, u8 color);
void (*error)(const char* info);
void (*exit)();
void (*exit)();
} callback;

struct
{
s16* samples;
TIC80_SAMPLETYPE* buffer;
s32 count;
} sound;
} samples;

u32* screen;
tic80_pixel_color_format screen_format;

u32 *screen;
} tic80;

typedef union
Expand Down Expand Up @@ -154,9 +155,9 @@ typedef struct

} tic80_input;

TIC80_API tic80* tic80_create(s32 samplerate);
TIC80_API tic80* tic80_create(s32 samplerate, tic80_pixel_color_format format);
TIC80_API void tic80_load(tic80* tic, void* cart, s32 size);
TIC80_API void tic80_tick(tic80* tic, const tic80_input* input);
TIC80_API void tic80_tick(tic80* tic, tic80_input input);
TIC80_API void tic80_sound(tic80* tic);
TIC80_API void tic80_delete(tic80* tic);

Expand Down
33 changes: 6 additions & 27 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include "tic.h"
#include "time.h"

// convenience macros to loop languages
#define FOR_EACH_LANG(ln) for (tic_script_config** conf = Languages ; *conf != NULL; conf++ ) { tic_script_config* ln = *conf;
Expand All @@ -42,9 +43,7 @@ typedef struct
ErrorOutput error;
ExitCallback exit;

u64 (*counter)(void*);
u64 (*freq)(void*);
u64 start;
clock_t start;

void* data;
} tic_tick_data;
Expand Down Expand Up @@ -813,8 +812,9 @@ TIC_API_LIST(TIC_API_DEF)

struct tic_mem
{
tic_ram ram;
tic_cartridge cart;
tic80 product;
tic_ram ram;
tic_cartridge cart;

char saveid[TIC_SAVEID_SIZE];

Expand All @@ -829,22 +829,9 @@ struct tic_mem

u8 data;
} input;

struct
{
s16* buffer;
s32 size;
} samples;

#if defined(_3DS)
u32 *screen;
#else
u32 screen[TIC80_FULLWIDTH * TIC80_FULLHEIGHT];
#endif
tic80_pixel_color_format screen_format;
};

tic_mem* tic_core_create(s32 samplerate);
tic_mem* tic_core_create(s32 samplerate, tic80_pixel_color_format format);
void tic_core_close(tic_mem* memory);
void tic_core_pause(tic_mem* memory);
void tic_core_resume(tic_mem* memory);
Expand All @@ -856,14 +843,6 @@ void tic_core_blit(tic_mem* tic);
void tic_core_blit_ex(tic_mem* tic, tic_blit_callback clb);
const tic_script_config* tic_core_script_config(tic_mem* memory);

typedef struct
{
tic80 tic;
tic_mem* memory;
tic_tick_data tickData;
u64 tick_counter;
} tic80_local;

#define VBANK(tic, bank) \
bool MACROVAR(_bank_) = tic_api_vbank(tic, bank); \
SCOPE(tic_api_vbank(tic, MACROVAR(_bank_)))
2 changes: 1 addition & 1 deletion src/api/js.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ static duk_ret_t duk_sfx(duk_context* duk)

s32 duration = duk_opt_int(duk, 2, -1);
s32 channel = duk_opt_int(duk, 3, 0);
s32 volumes[TIC_STEREO_CHANNELS];
s32 volumes[TIC80_SAMPLE_CHANNELS];

if(duk_is_array(duk, 4))
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ static s32 lua_sfx(lua_State* lua)
s32 octave = -1;
s32 duration = -1;
s32 channel = 0;
s32 volumes[TIC_STEREO_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 volumes[TIC80_SAMPLE_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 speed = SFX_DEF_SPEED;

s32 index = getLuaNumber(lua, 1);
Expand Down
6 changes: 3 additions & 3 deletions src/api/mruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ static mrb_value mrb_sfx(mrb_state* mrb, mrb_value self)
mrb_int duration = -1;
mrb_int channel = 0;
mrb_value volume = mrb_int_value(mrb, MAX_VOLUME);
mrb_int volumes[TIC_STEREO_CHANNELS];
mrb_int volumes[TIC80_SAMPLE_CHANNELS];
mrb_int speed = SFX_DEF_SPEED;

mrb_int argc = mrb_get_args(mrb, "i|oiio!i", &index, &note_obj, &duration, &channel, &volume, &speed);
Expand All @@ -653,14 +653,14 @@ static mrb_value mrb_sfx(mrb_state* mrb, mrb_value self)

if (mrb_array_p(volume))
{
for (mrb_int i = 0; i < TIC_STEREO_CHANNELS; ++i)
for (mrb_int i = 0; i < TIC80_SAMPLE_CHANNELS; ++i)
{
volumes[i] = mrb_integer(mrb_ary_entry(volume, i));
}
}
else if (mrb_fixnum_p(volume))
{
for (size_t ch = 0; ch < TIC_STEREO_CHANNELS; ++ch)
for (size_t ch = 0; ch < TIC80_SAMPLE_CHANNELS; ++ch)
{
volumes[ch] = mrb_integer(volume);
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/squirrel.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ static SQInteger squirrel_sfx(HSQUIRRELVM vm)
s32 octave = -1;
s32 duration = -1;
s32 channel = 0;
s32 volumes[TIC_STEREO_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 volumes[TIC80_SAMPLE_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 speed = SFX_DEF_SPEED;

s32 index = getSquirrelNumber(vm, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/api/wren.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ static void wren_sfx(WrenVM* vm)
s32 octave = -1;
s32 duration = -1;
s32 channel = 0;
s32 volumes[TIC_STEREO_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 volumes[TIC80_SAMPLE_CHANNELS] = {MAX_VOLUME, MAX_VOLUME};
s32 speed = SFX_DEF_SPEED;

if (index >= 0)
Expand Down
89 changes: 47 additions & 42 deletions src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void tic_api_sync(tic_mem* tic, u32 mask, s32 bank, bool toCart)
double tic_api_time(tic_mem* memory)
{
tic_core* core = (tic_core*)memory;
return (double)((core->data->counter(core->data->data) - core->data->start) * 1000) / core->data->freq(core->data->data);
return (clock() - core->data->start) * 1000.0 / CLOCKS_PER_SEC;
}

s32 tic_api_tstamp(tic_mem* memory)
Expand Down Expand Up @@ -275,7 +275,7 @@ static void soundClear(tic_mem* memory)
}

memset(&memory->ram.registers, 0, sizeof memory->ram.registers);
memset(memory->samples.buffer, 0, memory->samples.size);
memset(memory->product.samples.buffer, 0, memory->product.samples.count * TIC80_SAMPLESIZE);

tic_api_music(memory, -1, 0, 0, false, false, -1, -1);
}
Expand All @@ -290,6 +290,32 @@ static void resetVbank(tic_mem* memory)
memory->ram.vram.blit.segment = TIC_DEFAULT_BLIT_MODE;
}

static void font2ram(tic_mem* memory)
{
memory->ram.font = (tic_font)
{
.regular =
{
.data =
{
#include "font.inl"
},
.width = TIC_FONT_WIDTH,
.height = TIC_FONT_HEIGHT,
},

.alt =
{
.data =
{
#include "altfont.inl"
},
.width = TIC_ALTFONT_WIDTH,
.height = TIC_FONT_HEIGHT,
},
};
}

void tic_api_reset(tic_mem* memory)
{
tic_core* core = (tic_core*)memory;
Expand Down Expand Up @@ -324,32 +350,12 @@ void tic_api_reset(tic_mem* memory)

soundClear(memory);
updateSaveid(memory);
font2ram(memory);
}

static void cart2ram(tic_mem* memory)
{
memory->ram.font = (tic_font)
{
.regular =
{
.data =
{
#include "font.inl"
},
.width = TIC_FONT_WIDTH,
.height = TIC_FONT_HEIGHT,
},

.alt =
{
.data =
{
#include "altfont.inl"
},
.width = TIC_ALTFONT_WIDTH,
.height = TIC_FONT_HEIGHT,
},
};
font2ram(memory);

enum
{
Expand Down Expand Up @@ -442,7 +448,7 @@ void tic_core_tick(tic_mem* tic, tic_tick_data* data)
tic->input.keyboard = 1;
else tic->input.data = -1; // default is all enabled

data->start = data->counter(core->data->data);
data->start = clock();

done = tic_init_vm(core, code, config);
}
Expand Down Expand Up @@ -473,7 +479,7 @@ void tic_core_pause(tic_mem* memory)
if (core->data)
{
core->pause.time.start = core->data->start;
core->pause.time.paused = core->data->counter(core->data->data);
core->pause.time.paused = clock();
}
}

Expand All @@ -485,7 +491,7 @@ void tic_core_resume(tic_mem* memory)
{
memcpy(&core->state, &core->pause.state, sizeof(tic_core_state_data));
memcpy(&memory->ram, &core->pause.ram, sizeof(tic_ram));
core->data->start = core->pause.time.start + core->data->counter(core->data->data) - core->pause.time.paused;
core->data->start = core->pause.time.start + clock() - core->pause.time.paused;
}
}

Expand All @@ -500,7 +506,8 @@ void tic_core_close(tic_mem* memory)
blip_delete(core->blip.left);
blip_delete(core->blip.right);

free(memory->samples.buffer);
free(memory->product.screen);
free(memory->product.samples.buffer);
free(core);
}

Expand Down Expand Up @@ -580,8 +587,8 @@ static inline tic_vram* vbank1(tic_core* core)
static inline void updpal(tic_mem* tic, tic_blitpal* pal0, tic_blitpal* pal1)
{
tic_core* core = (tic_core*)tic;
*pal0 = tic_tool_palette_blit(&vbank0(core)->palette, tic->screen_format);
*pal1 = tic_tool_palette_blit(&vbank1(core)->palette, tic->screen_format);
*pal0 = tic_tool_palette_blit(&vbank0(core)->palette, core->screen_format);
*pal1 = tic_tool_palette_blit(&vbank1(core)->palette, core->screen_format);
}

static inline void updbdr(tic_mem* tic, s32 row, u32* ptr, tic_blit_callback clb, tic_blitpal* pal0, tic_blitpal* pal1)
Expand Down Expand Up @@ -621,7 +628,7 @@ void tic_core_blit_ex(tic_mem* tic, tic_blit_callback clb)
updpal(tic, &pal0, &pal1);

s32 row = 0;
u32* rowPtr = tic->screen;
u32* rowPtr = tic->product.screen;

#define UPDBDR() updbdr(tic, row, rowPtr, clb, &pal0, &pal1)

Expand Down Expand Up @@ -683,27 +690,25 @@ void tic_core_blit(tic_mem* tic)
tic_core_blit_ex(tic, (tic_blit_callback){scanline, border, NULL});
}

tic_mem* tic_core_create(s32 samplerate)
tic_mem* tic_core_create(s32 samplerate, tic80_pixel_color_format format)
{
tic_core* core = (tic_core*)malloc(sizeof(tic_core));
memset(core, 0, sizeof(tic_core));
*core = (tic_core){0};

if (core != (tic_core*)&core->memory)
{
free(core);
return NULL;
}
tic80* product = &core->memory.product;

core->memory.screen_format = TIC80_PIXEL_COLOR_RGBA8888;
core->screen_format = format;
core->samplerate = samplerate;
#ifdef _3DS
// To feed texture data directly to the 3DS GPU, linearly allocated memory is required, which is
// not guaranteed by malloc.
// Additionally, allocate TIC80_FULLHEIGHT + 1 lines to minimize glitches in linear scaling mode.
core->memory.screen = linearAlloc(TIC80_FULLWIDTH * (TIC80_FULLHEIGHT + 1) * sizeof(u32));
product->screen = linearAlloc(TIC80_FULLWIDTH * (TIC80_FULLHEIGHT + 1) * sizeof(u32));
#else
product->screen = malloc(TIC80_FULLWIDTH * TIC80_FULLHEIGHT * sizeof product->screen[0]);
#endif
core->memory.samples.size = samplerate * TIC_STEREO_CHANNELS / TIC80_FRAMERATE * sizeof(s16);
core->memory.samples.buffer = malloc(core->memory.samples.size);
product->samples.count = samplerate * TIC80_SAMPLE_CHANNELS / TIC80_FRAMERATE;
product->samples.buffer = malloc(product->samples.count * TIC80_SAMPLESIZE);

core->blip.left = blip_new(samplerate / 10);
core->blip.right = blip_new(samplerate / 10);
Expand Down
5 changes: 3 additions & 2 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ typedef struct
typedef struct
{
tic_mem memory; // it should be first
tic80_pixel_color_format screen_format;

void* currentVM;
const tic_script_config* currentScript;
Expand All @@ -188,8 +189,8 @@ typedef struct

struct
{
u64 start;
u64 paused;
clock_t start;
clock_t paused;
} time;
} pause;

Expand Down
4 changes: 2 additions & 2 deletions src/core/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ void tic_core_synth_sound(tic_mem* memory)
stereo_synthesize(core, core->state.registers.left, core->blip.left, 0);
stereo_synthesize(core, core->state.registers.right, core->blip.right, 1);

blip_read_samples(core->blip.left, core->memory.samples.buffer, core->samplerate / TIC80_FRAMERATE, TIC_STEREO_CHANNELS);
blip_read_samples(core->blip.right, core->memory.samples.buffer + 1, core->samplerate / TIC80_FRAMERATE, TIC_STEREO_CHANNELS);
blip_read_samples(core->blip.left, core->memory.product.samples.buffer, core->samplerate / TIC80_FRAMERATE, TIC80_SAMPLE_CHANNELS);
blip_read_samples(core->blip.right, core->memory.product.samples.buffer + 1, core->samplerate / TIC80_FRAMERATE, TIC80_SAMPLE_CHANNELS);

// if the head has advanced, we can advance the tail too. Otherwise, we just
// keep synthesizing audio using the last known register values, so at least we don't get crackles
Expand Down
2 changes: 1 addition & 1 deletion src/studio/screens/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ static void onExport_mapimg(Console* console, const char* param, const char* pat
for(s32 j = 0; j < TIC80_HEIGHT; j++)
for(s32 i = 0; i < TIC80_WIDTH; i++)
img.values[(i + c * TIC80_WIDTH) + (j + r * TIC80_HEIGHT) * Width] =
tic->screen[(i + TIC80_MARGIN_LEFT) + (j + TIC80_MARGIN_TOP) * TIC80_FULLWIDTH];
tic->product.screen[(i + TIC80_MARGIN_LEFT) + (j + TIC80_MARGIN_TOP) * TIC80_FULLWIDTH];
}
}

Expand Down
Loading

0 comments on commit 3dc2fea

Please sign in to comment.