Skip to content

Commit

Permalink
ZX 128K MIDI port emulation (but no output).
Browse files Browse the repository at this point in the history
  • Loading branch information
jxsvoboda committed Aug 20, 2018
1 parent 717d5e8 commit 8aa1009
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ sources_generic = \
fileutil.c \
gzx.c \
memio.c \
midi.c \
z80.c \
z80dep.c \
rs232.c \
snap.c \
snap_ay.c \
strutil.c \
Expand Down
14 changes: 12 additions & 2 deletions ay.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "ay.h"

Expand Down Expand Up @@ -50,6 +49,17 @@ static void reset_env_gen(ay_t *ay)
}
}

/** Write AY I/O port.
*
* @param ay AY
* @param val Value
*/
static void ay_io_port_write(ay_t *ay, uint8_t val)
{
if (ay->ioport_write != NULL)
ay->ioport_write(ay->ioport_write_arg, val);
}

/** Write AY register.
*
* @param ay AY
Expand All @@ -59,10 +69,10 @@ void ay_reg_write(ay_t *ay, uint8_t val)
{
switch (ay->cur_reg) {
case ay_rn_esccr: reset_env_gen(ay); break;
case ay_rn_io_a: ay_io_port_write(ay, val); break;
}

ay->reg[ay->cur_reg] = val;
// printf("ay_reg_write %d,0x%02x\n", ay->cur_reg, val);
}

/** Read currently selected AY register.
Expand Down
3 changes: 3 additions & 0 deletions ay.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ typedef struct {
uint16_t noise_cnt;
uint8_t noise_smp;
uint32_t d_clocks;

void (*ioport_write)(void *, uint8_t);
void *ioport_write_arg;
} ay_t;

extern void ay_reg_select(ay_t *, uint8_t);
Expand Down
1 change: 1 addition & 0 deletions clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define CLOCK_H

#define Z80_CLOCK 3500000L
#define MIDI_BAUD 31250

#endif
27 changes: 27 additions & 0 deletions gzx.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "clock.h"
#include "intdef.h"
#include "memio.h"
#include "mgfx.h"
Expand All @@ -22,11 +23,13 @@
#include "z80.h"
#include "zx_kbd.h"
#include "zx_scr.h"
#include "rs232.h"
#include "snap.h"
#include "zx_sound.h"
#include "zx_tape.h"
#include "ay.h"
#include "menus.h"
#include "midi.h"
#include "debug.h"
#include "z80g.h"
#include "zx.h"
Expand Down Expand Up @@ -220,6 +223,22 @@ void zx_scr_save(void) {
fclose(f);
}

/** Value was written to AY I/O port.
*
* @param arg Callback argument
* @param val Value
*/
static void gzx_ay_ioport_write(void *arg, uint8_t val)
{
rs232_write(&rs232, val);
}

/** Character was sent via RS-232 port */
static void gzx_rs232_sendchar(void *arg, uint8_t val)
{
midi_port_write(&midi, val);
}

static unsigned long snd_t,tapp_t;

void zx_reset(void) {
Expand Down Expand Up @@ -263,8 +282,16 @@ static int zx_init(void) {
if(zx_keys_init()<0) return -1;
printf("sound\n");
if(zx_sound_init()<0) return -1;

printf("ay\n");
if(ay_init(&ay0, 125/*d_t_states*/)<0) return -1;
ay0.ioport_write = gzx_ay_ioport_write;
ay0.ioport_write_arg = &ay0;

rs232_init(&rs232, Z80_CLOCK / MIDI_BAUD);
rs232.sendchar = gzx_rs232_sendchar;
rs232.sendchar_arg = &rs232;

if(zx_tape_init(79)<0) return -1;
//if(zx_tape_selectfile("/mnt/dos/jetpac.tap")<0) return -1;

Expand Down
15 changes: 15 additions & 0 deletions midi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* GZX - George's ZX Spectrum Emulator
* ZX Spectrum 128K MIDI port emulation
*/
#include <stdio.h>
#include "midi.h"

void midi_port_init(midi_port_t *mp)
{
}

void midi_port_write(midi_port_t *mp, uint8_t val)
{
printf("MIDI port write: 0x%02x\n", val);
}
16 changes: 16 additions & 0 deletions midi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* GZX - George's ZX Spectrum Emulator
* ZX Spectrum 128K MIDI port emulation
*/
#ifndef MIDI_PORT_H
#define MIDI_PORT_H

#include <stdint.h>

typedef struct {
} midi_port_t;

extern void midi_port_init(midi_port_t *);
extern void midi_port_write(midi_port_t *, uint8_t);

#endif
32 changes: 32 additions & 0 deletions rs232.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* GZX - George's ZX Spectrum Emulator
* ZX Spectrum 128K RS-232 port emulation
*/
#include <stdio.h>
#include "rs232.h"

void rs232_init(rs232_t *rs, uint32_t d_clocks)
{
rs->d_clocks = d_clocks;
rs->state = rs232_idle;
}

void rs232_write(rs232_t *rs, uint8_t val)
{
uint8_t b;

b = (val >> rs232_txd) & 0x1;

if (rs->state == rs232_idle && (b == 0x0)) {
rs->state = rs232_sending;
rs->bit_no = 0;
rs->buf = 0x0;
} else if (rs->state == rs232_sending) {
rs->buf = (rs->buf >> 1) | (b << 7);
if (++rs->bit_no >= 8) {
rs->state = rs232_idle;
if (rs->sendchar != NULL)
rs->sendchar(rs->sendchar_arg, rs->buf);
}
}
}
38 changes: 38 additions & 0 deletions rs232.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* GZX - George's ZX Spectrum Emulator
* ZX Spectrum 128K RS-232 port emulation
*/
#ifndef RS232_H
#define RS232_H

#include <stdint.h>

/* Bits within the RS232 register */
enum {
rs232_txd = 2
};

typedef enum {
rs232_idle,
rs232_sending
} rs232_state_t;

typedef struct {
/** Z80 T states between successive bits (rel. to baud rate) */
uint32_t d_clocks;
/** State */
rs232_state_t state;
/** Bit number */
uint8_t bit_no;
/** Transfer buffer */
uint8_t buf;
/** A symbol was sent */
void (*sendchar)(void *, uint8_t);
/** Argument to sendchar callback */
void *sendchar_arg;
} rs232_t;

extern void rs232_init(rs232_t *, uint32_t);
extern void rs232_write(rs232_t *, uint8_t);

#endif
8 changes: 8 additions & 0 deletions zx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
*/

#include "ay.h"
#include "rs232.h"
#include "zx.h"

/** First AY */
ay_t ay0;

/** MIDI port */
midi_port_t midi;

/** RS-232 port */
rs232_t rs232;
4 changes: 4 additions & 0 deletions zx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#define ZX_H

#include "ay.h"
#include "midi.h"
#include "rs232.h"

extern ay_t ay0;
extern midi_port_t midi;
extern rs232_t rs232;

#endif

0 comments on commit 8aa1009

Please sign in to comment.