Skip to content

Commit

Permalink
Move serial emulation to separate file; other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jblang committed May 26, 2018
1 parent 9a467ff commit 64bdb79
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BIN=z80ctrl
FF_OBJS=diskio.o ff.o mmc_avr_spi.o
OBJS=uart.o spi.o iox.o bus.o z80.o iorq.o diskemu.o cli.o ihex.o disasm.o util.o $(FF_OBJS)
OBJS=uart.o spi.o iox.o bus.o z80.o iorq.o sioemu.o diskemu.o cli.o ihex.o disasm.o util.o $(FF_OBJS)

CC=avr-gcc
OBJCOPY=avr-objcopy
Expand Down
44 changes: 22 additions & 22 deletions diskemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ uint8_t dpb[] = {
#define CMDLEN 6

uint8_t hdsk_command;
uint8_t hdsk_index;
uint8_t drive_dma_resultdex;
uint8_t dma_disk;
uint8_t dma_sector;
uint16_t dma_track;
Expand Down Expand Up @@ -553,7 +553,7 @@ void hdsk_dma_read()
/**
* Perform DMA disk write
*/
void hdsk_dma_write()
void drive_dma_write()
{
UINT bw;
FRESULT fr;
Expand All @@ -577,22 +577,22 @@ void hdsk_dma_write()
/**
* Initiate command and return status read from hard drive port
*/
uint8_t hdsk_in()
uint8_t drive_dma_result()
{
uint8_t result = CPM_ERROR;
if ((hdsk_index == CMDLEN) && ((hdsk_command == HDSK_READ) || (hdsk_command == HDSK_WRITE))) {
if ((drive_dma_resultdex == CMDLEN) && ((hdsk_command == HDSK_READ) || (hdsk_command == HDSK_WRITE))) {
if (hdsk_command == HDSK_READ)
dma_function = &hdsk_dma_read;
else
dma_function = &hdsk_dma_write;
dma_function = &drive_dma_write;
hdsk_command = HDSK_NONE;
hdsk_index = 0;
drive_dma_resultdex = 0;
result = CPM_OK;
} else if (hdsk_command == HDSK_PARAM) {
result = dpb[hdsk_index++];
if (hdsk_index >= DPBLEN) {
result = dpb[drive_dma_resultdex++];
if (drive_dma_resultdex >= DPBLEN) {
hdsk_command = HDSK_NONE;
hdsk_index = 0;
drive_dma_resultdex = 0;
}
}
return result;
Expand All @@ -601,50 +601,50 @@ uint8_t hdsk_in()
/**
* Set up command written to hard drive port
*/
void hdsk_out(uint8_t data)
void drive_dma_command(uint8_t data)
{
if (hdsk_command == HDSK_PARAM) {
hdsk_index = 0;
drive_dma_resultdex = 0;
} else if (hdsk_command == HDSK_READ || hdsk_command == HDSK_WRITE) {
if (hdsk_index < CMDLEN) {
switch(hdsk_index) {
if (drive_dma_resultdex < CMDLEN) {
switch(drive_dma_resultdex) {
case 0:
dma_disk = data;
hdsk_index++;
drive_dma_resultdex++;
break;
case 1:
dma_sector = data;
hdsk_index++;
drive_dma_resultdex++;
break;
case 2:
dma_track = data;
hdsk_index++;
drive_dma_resultdex++;
break;
case 3:
dma_track += (data << 8);
hdsk_index++;
drive_dma_resultdex++;
break;
case 4:
dma_addr = data;
hdsk_index++;
drive_dma_resultdex++;
break;
case 5:
dma_addr += (data << 8);
hdsk_index++;
drive_dma_resultdex++;
break;
default:
hdsk_command = HDSK_NONE;
hdsk_index = 0;
drive_dma_resultdex = 0;
} } else {
hdsk_command = HDSK_NONE;
hdsk_index = 0;
drive_dma_resultdex = 0;
}
} else {
if ((HDSK_RESET <= data) && (data <= HDSK_PARAM)) {
hdsk_command = data;
} else {
hdsk_command = HDSK_RESET;
}
hdsk_index = 0;
drive_dma_resultdex = 0;
}
}
5 changes: 3 additions & 2 deletions diskemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define DRIVE_STATUS 0x8
#define DRIVE_CONTROL 0x9
#define DRIVE_DATA 0xA
#define DRIVE_DMA 0xFD

int drive_bootload();
void drive_unmount(uint8_t drv);
Expand All @@ -42,7 +43,7 @@ uint8_t drive_sector(void);
void drive_write(uint8_t data);
uint8_t drive_read(void);

uint8_t hdsk_in();
void hdsk_out(uint8_t data);
uint8_t drive_dma_result();
void drive_dma_command(uint8_t data);

#endif
35 changes: 8 additions & 27 deletions iorq.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "uart.h"
#include "bus.h"
#include "diskemu.h"
#include "sioemu.h"

#include <avr/interrupt.h>

Expand All @@ -39,29 +40,9 @@ void (*dma_function)(void) = NULL;
/**
* IO registers
*/
#define SIO0_STATUS 0x10
#define SIO0_DATA 0x11
#define SIO1_STATUS 0x12
#define SIO1_DATA 0x13
#define SIOA_CONTROL 0x80
#define SIOA_DATA 0x81
#define SIOB_CONTROL 0x82
#define SIOB_DATA 0x83
#define HDISK_IO 0xFD
#define SIMH_DEV 0xFE
#define SENSE_SW 0xFF

/**
* Physical to virtual UART mapping.
*/
uint8_t z80_uart[] = {0 , 1};

/**
* Utility macros to generate SIO status register values
*/
#define ALTAIR_SIO_STATUS(u) ((((uart_testtx(z80_uart[(u)]) == 0) << 1) & 0x2) | ((uart_testrx(z80_uart[(u)]) > 0) & 0x1))
#define ZILOG_SIO_STATUS(u) ((1 << 3) | (1 << 5) | (((uart_testtx(z80_uart[(u)]) == 0) << 2) & 0x4) | ((uart_testrx(z80_uart[(u)]) > 0) & 0x1))

/**
* Handle Z80 IO request
*/
Expand All @@ -71,25 +52,25 @@ void iorq_dispatch(void)
switch (GET_ADDRLO) {
case SIO0_STATUS:
if (!GET_RD) {
SET_DATA(ALTAIR_SIO_STATUS(0));
SET_DATA(ACIA_STATUS(0));
DATA_OUTPUT;
}
break;
case SIO1_STATUS:
if (!GET_RD) {
SET_DATA(ALTAIR_SIO_STATUS(1));
SET_DATA(ACIA_STATUS(1));
DATA_OUTPUT;
}
break;
case SIOA_CONTROL:
if (!GET_RD) {
SET_DATA(ZILOG_SIO_STATUS(0));
SET_DATA(ZSIO_STATUS(0));
DATA_OUTPUT;
}
break;
case SIOB_CONTROL:
if (!GET_RD) {
SET_DATA(ZILOG_SIO_STATUS(1));
SET_DATA(ZSIO_STATUS(1));
DATA_OUTPUT;
}
break;
Expand Down Expand Up @@ -135,12 +116,12 @@ void iorq_dispatch(void)
drive_write(GET_DATA);
}
break;
case HDISK_IO:
case DRIVE_DMA:
if (!GET_RD) {
SET_DATA(hdsk_in());
SET_DATA(drive_dma_result());
DATA_OUTPUT;
} else if (!GET_WR) {
hdsk_out(GET_DATA);
drive_dma_command(GET_DATA);
}
break;
default:
Expand Down
34 changes: 34 additions & 0 deletions sioemu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* z80ctrl (https://github.com/jblang/z80ctrl)
* Copyright 2018 J.B. Langston
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

/**
* @file sioemu.c Serial I/O emulation
*/

#include <stdint.h>

#include "sioemu.h"

/**
* Physical to virtual UART mapping.
*/
uint8_t z80_uart[] = {0 , 1};
47 changes: 47 additions & 0 deletions sioemu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* z80ctrl (https://github.com/jblang/z80ctrl)
* Copyright 2018 J.B. Langston
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

/**
* @file sioemu.h Serial I/O emulation
*/

#ifndef SIOEMU_H
#define SIOEMU_H

#define SIO0_STATUS 0x10
#define SIO0_DATA 0x11
#define SIO1_STATUS 0x12
#define SIO1_DATA 0x13
#define SIOA_CONTROL 0x80
#define SIOA_DATA 0x81
#define SIOB_CONTROL 0x82
#define SIOB_DATA 0x83

/**
* Utility macros to generate SIO status register values
*/
#define ACIA_STATUS(u) ((((uart_testtx(z80_uart[(u)]) == 0) << 1) & 0x2) | ((uart_testrx(z80_uart[(u)]) > 0) & 0x1))
#define ZSIO_STATUS(u) ((1 << 3) | (1 << 5) | (((uart_testtx(z80_uart[(u)]) == 0) << 2) & 0x4) | ((uart_testrx(z80_uart[(u)]) > 0) & 0x1))

extern uint8_t z80_uart[];

#endif

0 comments on commit 64bdb79

Please sign in to comment.