Skip to content

Commit

Permalink
some changes for review
Browse files Browse the repository at this point in the history
  • Loading branch information
mooinglemur committed Oct 6, 2024
1 parent ba8af95 commit 7263001
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 85 deletions.
23 changes: 1 addition & 22 deletions src/cpu/registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "../endian.h"

#define FLAG_CARRY 0x01
#define FLAG_ZERO 0x02
Expand All @@ -20,26 +21,6 @@

//6502 CPU registers

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t low; \
uint8_t high; \
}; \
uint16_t name; \
}
#else
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t high; \
uint8_t low; \
}; \
uint16_t name; \
}
#endif

struct regs
{
LOW_HIGH_UNION(c, a, b);
Expand All @@ -59,8 +40,6 @@ struct regs
bool is65c816;
};

#undef LOW_HIGH_UNION

void increment_wrap_at_page_boundary(uint16_t *value);
void decrement_wrap_at_page_boundary(uint16_t *value);
uint16_t direct_page_add(uint16_t offset);
Expand Down
21 changes: 21 additions & 0 deletions src/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t low; \
uint8_t high; \
}; \
uint16_t name; \
}
#else
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t high; \
uint8_t low; \
}; \
uint16_t name; \
}
#endif
59 changes: 19 additions & 40 deletions src/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "glue.h"
#include "midi.h"
#include "audio.h"
#include "endian.h"

#ifdef _WIN32
#include <windows.h>
Expand All @@ -32,26 +33,6 @@ enum MIDI_states {
SYSEX,
};

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t low; \
uint8_t high; \
}; \
uint16_t name; \
}
#else
#define LOW_HIGH_UNION(name, low, high) \
union { \
struct { \
uint8_t high; \
uint8_t low; \
}; \
uint16_t name; \
}
#endif

struct midi_serial_regs
{
LOW_HIGH_UNION(dl, dll, dlm);
Expand Down Expand Up @@ -123,8 +104,6 @@ struct midi_serial_regs
pthread_mutexattr_t fifo_mutex_attr;
};

#undef LOW_HIGH_UNION

struct midi_serial_regs mregs[2];
static bool serial_midi_mutexes_initialized = false;

Expand Down Expand Up @@ -434,60 +413,60 @@ int handle_midi_event(void* data, fluid_midi_event_t* event)
int buflen;

switch (type) {
case NOTE_OFF:
case NOTE_ON:
case FS_NOTE_OFF:
case FS_NOTE_ON:
key = dl_fluid_midi_event_get_key(event);
val = dl_fluid_midi_event_get_velocity(event);
midi_event_enqueue_normal(mrp, cmd, key, val);
break;
case KEY_PRESSURE:
case FS_KEY_PRESSURE:
key = dl_fluid_midi_event_get_key(event);
val = dl_fluid_midi_event_get_value(event);
midi_event_enqueue_normal(mrp, cmd, key, val);
break;
case CONTROL_CHANGE:
case FS_CONTROL_CHANGE:
key = dl_fluid_midi_event_get_control(event);
val = dl_fluid_midi_event_get_value(event);
midi_event_enqueue_normal(mrp, cmd, key, val);
break;
case PITCH_BEND:
case FS_PITCH_BEND:
key = dl_fluid_midi_event_get_pitch(event) & 0x7f;
val = (dl_fluid_midi_event_get_pitch(event) >> 7) & 0x7f;
midi_event_enqueue_normal(mrp, cmd, key, val);
break;
case PROGRAM_CHANGE:
case CHANNEL_PRESSURE:
case FS_PROGRAM_CHANGE:
case FS_CHANNEL_PRESSURE:
val = dl_fluid_midi_event_get_program(event);
midi_event_enqueue_short(mrp, cmd, val);
break;
case MIDI_TIME_CODE:
case FS_MIDI_TIME_CODE:
val = dl_fluid_midi_event_get_value(event);
midi_event_enqueue_short(mrp, type, val);
mrp->in_midi_last_command = 0;
break;
case MIDI_TUNE_REQUEST:
case FS_MIDI_TUNE_REQUEST:
case 0xF4:
case 0xF5:
midi_event_enqueue_byte(mrp, type);
mrp->in_midi_last_command = 0;
break;
case MIDI_SYNC:
case MIDI_TICK:
case MIDI_START:
case MIDI_CONTINUE:
case MIDI_STOP:
case MIDI_ACTIVE_SENSING:
case MIDI_SYSTEM_RESET:
case FS_MIDI_SYNC:
case FS_MIDI_TICK:
case FS_MIDI_START:
case FS_MIDI_CONTINUE:
case FS_MIDI_STOP:
case FS_MIDI_ACTIVE_SENSING:
case FS_MIDI_SYSTEM_RESET:
midi_event_enqueue_byte(mrp, type);
break;
case MIDI_SYSEX:
case FS_MIDI_SYSEX:
// FluidSynth doesn't offer a get_sysex function, but internally
// a text event is equivalent to a sysex event, in terms of what
// parts of the event structure are populated
//
// Unfortunately that means we have to fool it in order to
// access the data.
dl_fluid_midi_event_set_type(event, MIDI_TEXT);
dl_fluid_midi_event_set_type(event, FS_MIDI_TEXT);
if (dl_fluid_midi_event_get_text(event, (void **)&bufptr, &buflen) == FLUID_OK && bufptr != NULL) {
midi_event_enqueue_sysex(mrp, bufptr, buflen);
}
Expand Down
46 changes: 23 additions & 23 deletions src/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
#define MIDI_UART_OSC_RATE_MHZ 16.0f
#define MIDI_UART_PRIMARY_DIVIDER 16

#define MIDI_TEXT 0x01
#define NOTE_OFF 0x80
#define NOTE_ON 0x90
#define KEY_PRESSURE 0xa0
#define CONTROL_CHANGE 0xb0
#define PROGRAM_CHANGE 0xc0
#define CHANNEL_PRESSURE 0xd0
#define PITCH_BEND 0xe0
#define MIDI_SYSEX 0xf0
#define MIDI_TIME_CODE 0xf1
#define MIDI_SONG_POSITION 0xf2
#define MIDI_SONG_SELECT 0xf3
#define MIDI_TUNE_REQUEST 0xf6
#define MIDI_EOX 0xf7
#define MIDI_SYNC 0xf8
#define MIDI_TICK 0xf9
#define MIDI_START 0xfa
#define MIDI_CONTINUE 0xfb
#define MIDI_STOP 0xfc
#define MIDI_ACTIVE_SENSING 0xfe
#define MIDI_SYSTEM_RESET 0xff
#define FS_MIDI_TEXT 0x01
#define FS_NOTE_OFF 0x80
#define FS_NOTE_ON 0x90
#define FS_KEY_PRESSURE 0xa0
#define FS_CONTROL_CHANGE 0xb0
#define FS_PROGRAM_CHANGE 0xc0
#define FS_CHANNEL_PRESSURE 0xd0
#define FS_PITCH_BEND 0xe0
#define FS_MIDI_SYSEX 0xf0
#define FS_MIDI_TIME_CODE 0xf1
#define FS_MIDI_SONG_POSITION 0xf2
#define FS_MIDI_SONG_SELECT 0xf3
#define FS_MIDI_TUNE_REQUEST 0xf6
#define FS_MIDI_EOX 0xf7
#define FS_MIDI_SYNC 0xf8
#define FS_MIDI_TICK 0xf9
#define FS_MIDI_START 0xfa
#define FS_MIDI_CONTINUE 0xfb
#define FS_MIDI_STOP 0xfc
#define FS_MIDI_ACTIVE_SENSING 0xfe
#define FS_MIDI_SYSTEM_RESET 0xff


void midi_init();
void midi_serial_init();
void midi_init(void);
void midi_serial_init(void);
void midi_serial_step(int clocks);
uint8_t midi_serial_read(uint8_t reg, bool debugOn);
void midi_serial_write(uint8_t reg, uint8_t val);
Expand Down

0 comments on commit 7263001

Please sign in to comment.