2016-01-05 04:12:47 +00:00
|
|
|
|
//
|
|
|
|
|
// Electron.cpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 03/01/2016.
|
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include "Electron.hpp"
|
|
|
|
|
|
2016-01-07 02:09:49 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2016-01-05 04:12:47 +00:00
|
|
|
|
using namespace Electron;
|
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
static const int cycles_per_line = 128;
|
|
|
|
|
static const int cycles_per_frame = 312*cycles_per_line;
|
|
|
|
|
static const int crt_cycles_multiplier = 8;
|
|
|
|
|
static const int crt_cycles_per_line = crt_cycles_multiplier * cycles_per_line;
|
|
|
|
|
|
|
|
|
|
Machine::Machine() :
|
|
|
|
|
_interruptControl(0),
|
|
|
|
|
_frameCycles(0),
|
|
|
|
|
_outputPosition(0)
|
2016-01-05 04:12:47 +00:00
|
|
|
|
{
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_crt = new Outputs::CRT(crt_cycles_per_line, 312, 1, 1);
|
2016-01-08 02:01:13 +00:00
|
|
|
|
_interruptStatus = 0x02;
|
2016-01-08 03:26:49 +00:00
|
|
|
|
setup6502();
|
2016-01-05 04:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Machine::~Machine()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int Machine::perform_bus_operation(CPU6502::BusOperation operation, uint16_t address, uint8_t *value)
|
|
|
|
|
{
|
2016-01-10 01:26:40 +00:00
|
|
|
|
unsigned int cycles = 1;
|
|
|
|
|
|
2016-01-10 17:30:24 +00:00
|
|
|
|
if(address < 0x8000)
|
2016-01-07 02:09:49 +00:00
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
{
|
2016-01-08 03:26:49 +00:00
|
|
|
|
*value = _ram[address];
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-08 03:26:49 +00:00
|
|
|
|
_ram[address] = *value;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
// TODO: range check on address; a lot of the time the machine will be running code outside of
|
|
|
|
|
// the screen area, meaning that no update is required.
|
|
|
|
|
// if (address
|
|
|
|
|
update_display();
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
2016-01-08 03:26:49 +00:00
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
// TODO: RAM timing for Modes 0–3
|
|
|
|
|
cycles += (_frameCycles&1)^1;
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-10 17:30:24 +00:00
|
|
|
|
if(address >= 0xc000)
|
2016-01-07 02:09:49 +00:00
|
|
|
|
{
|
2016-01-08 01:36:27 +00:00
|
|
|
|
if((address & 0xff00) == 0xfe00)
|
|
|
|
|
{
|
2016-01-10 01:26:40 +00:00
|
|
|
|
// printf("%c: %02x: ", isReadOperation(operation) ? 'r' : 'w', *value);
|
2016-01-08 01:36:27 +00:00
|
|
|
|
|
|
|
|
|
switch(address&0xf)
|
|
|
|
|
{
|
|
|
|
|
case 0x0:
|
2016-01-08 02:01:13 +00:00
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
{
|
|
|
|
|
*value = _interruptStatus;
|
|
|
|
|
_interruptStatus &= ~0x02;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interruptControl = *value;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
evaluate_interrupts();
|
2016-01-08 02:01:13 +00:00
|
|
|
|
}
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0x1:
|
|
|
|
|
break;
|
|
|
|
|
case 0x2:
|
2016-01-10 03:52:08 +00:00
|
|
|
|
_startScreenAddress = (_startScreenAddress & 0xfe00) | (uint16_t)(((*value) & 0xe0) << 1);
|
2016-01-08 02:01:13 +00:00
|
|
|
|
break;
|
2016-01-08 01:36:27 +00:00
|
|
|
|
case 0x3:
|
2016-01-10 03:52:08 +00:00
|
|
|
|
_startScreenAddress = (_startScreenAddress & 0x01ff) | (uint16_t)(((*value) & 0x3f) << 9);
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0x4:
|
|
|
|
|
printf("Cassette\n");
|
|
|
|
|
break;
|
|
|
|
|
case 0x5:
|
2016-01-08 02:01:13 +00:00
|
|
|
|
if(!isReadOperation(operation))
|
|
|
|
|
{
|
2016-01-10 17:30:24 +00:00
|
|
|
|
const uint8_t interruptDisable = (*value)&0xf0;
|
|
|
|
|
if( interruptDisable )
|
2016-01-08 02:01:13 +00:00
|
|
|
|
{
|
2016-01-10 17:30:24 +00:00
|
|
|
|
if( interruptDisable&0x10 ) _interruptStatus &= ~InterruptDisplayEnd;
|
|
|
|
|
if( interruptDisable&0x20 ) _interruptStatus &= ~InterruptRealTimeClock;
|
|
|
|
|
if( interruptDisable&0x40 ) _interruptStatus &= ~InterruptHighToneDetect;
|
|
|
|
|
evaluate_interrupts();
|
|
|
|
|
// TODO: NMI (?)
|
|
|
|
|
}
|
|
|
|
|
// else
|
|
|
|
|
{
|
|
|
|
|
uint8_t nextROM = (*value)&0xf;
|
|
|
|
|
|
|
|
|
|
// if(nextROM&0x08)
|
|
|
|
|
// {
|
|
|
|
|
// _activeRom = (Electron::ROMSlot)(nextROM&0x0e);
|
|
|
|
|
// printf("%d -> Paged %d\n", nextROM, _activeRom);
|
|
|
|
|
// }
|
|
|
|
|
if((_activeRom&12) != 8 || nextROM&8)
|
|
|
|
|
{
|
|
|
|
|
_activeRom = (Electron::ROMSlot)nextROM;
|
|
|
|
|
}
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// printf("Ignored!");
|
|
|
|
|
// }
|
|
|
|
|
// printf("%d -> Paged %d\n", nextROM, _activeRom);
|
2016-01-08 02:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0x6:
|
|
|
|
|
printf("Counter\n");
|
|
|
|
|
break;
|
|
|
|
|
case 0x7:
|
|
|
|
|
printf("Misc. control\n");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-01-10 01:26:40 +00:00
|
|
|
|
update_display();
|
|
|
|
|
// printf("Palette\n");
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(isReadOperation(operation))
|
2016-01-10 17:30:24 +00:00
|
|
|
|
*value = _os[address & 16383];
|
2016-01-08 01:36:27 +00:00
|
|
|
|
}
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-08 02:01:13 +00:00
|
|
|
|
if(isReadOperation(operation))
|
|
|
|
|
{
|
|
|
|
|
switch(_activeRom)
|
|
|
|
|
{
|
|
|
|
|
case ROMSlotBASIC:
|
|
|
|
|
case ROMSlotBASIC+1:
|
2016-01-10 17:30:24 +00:00
|
|
|
|
*value = _basic[address & 16383];
|
2016-01-08 02:01:13 +00:00
|
|
|
|
break;
|
|
|
|
|
case ROMSlotKeyboard:
|
|
|
|
|
case ROMSlotKeyboard+1:
|
2016-01-10 17:30:24 +00:00
|
|
|
|
*value = 0xf0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
*value = 0xff;
|
2016-01-08 02:01:13 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_frameCycles += cycles;
|
|
|
|
|
if(_frameCycles == cycles_per_frame)
|
|
|
|
|
{
|
|
|
|
|
update_display();
|
|
|
|
|
_frameCycles = 0;
|
|
|
|
|
}
|
|
|
|
|
if(_frameCycles == 128*128) signal_interrupt(InterruptRealTimeClock);
|
|
|
|
|
if(_frameCycles == 284*128) signal_interrupt(InterruptDisplayEnd);
|
|
|
|
|
|
|
|
|
|
return cycles;
|
2016-01-05 04:12:47 +00:00
|
|
|
|
}
|
2016-01-07 02:09:49 +00:00
|
|
|
|
|
|
|
|
|
void Machine::set_rom(ROMSlot slot, size_t length, const uint8_t *data)
|
|
|
|
|
{
|
|
|
|
|
uint8_t *target = nullptr;
|
|
|
|
|
switch(slot)
|
|
|
|
|
{
|
2016-01-08 03:26:49 +00:00
|
|
|
|
case ROMSlotBASIC: target = _basic; break;
|
|
|
|
|
case ROMSlotOS: target = _os; break;
|
2016-01-08 02:01:13 +00:00
|
|
|
|
default: return;
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(target, data, std::min((size_t)16384, length));
|
|
|
|
|
}
|
2016-01-10 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
inline void Machine::signal_interrupt(Electron::Interrupt interrupt)
|
|
|
|
|
{
|
|
|
|
|
_interruptStatus |= (interrupt << 2);
|
|
|
|
|
evaluate_interrupts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void Machine::evaluate_interrupts()
|
|
|
|
|
{
|
|
|
|
|
if(_interruptStatus & _interruptControl)
|
|
|
|
|
{
|
|
|
|
|
_interruptStatus |= 1;
|
|
|
|
|
}
|
|
|
|
|
set_irq_line(_interruptStatus & 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void Machine::update_display()
|
|
|
|
|
{
|
|
|
|
|
const int end_of_hsync = 3 * cycles_per_line;
|
|
|
|
|
|
|
|
|
|
if(_frameCycles >= end_of_hsync)
|
|
|
|
|
{
|
2016-01-10 02:53:33 +00:00
|
|
|
|
// assert sync for the first three lines of the display, with a break at the end for horizontal alignment
|
2016-01-10 01:26:40 +00:00
|
|
|
|
if(_outputPosition < end_of_hsync)
|
|
|
|
|
{
|
2016-01-10 02:53:33 +00:00
|
|
|
|
for(int c = 0; c < 3; c++)
|
|
|
|
|
{
|
2016-01-10 02:54:31 +00:00
|
|
|
|
_crt->output_sync(119 * crt_cycles_multiplier);
|
2016-01-10 02:53:33 +00:00
|
|
|
|
_crt->output_blank(9 * crt_cycles_multiplier);
|
|
|
|
|
}
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_outputPosition = end_of_hsync;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-10 01:34:22 +00:00
|
|
|
|
while(_outputPosition >= end_of_hsync && _outputPosition < _frameCycles)
|
2016-01-10 01:26:40 +00:00
|
|
|
|
{
|
|
|
|
|
const int current_line = _outputPosition >> 7;
|
|
|
|
|
const int line_position = _outputPosition & 127;
|
|
|
|
|
|
|
|
|
|
// all lines then start with 9 cycles of sync
|
|
|
|
|
if(!line_position)
|
|
|
|
|
{
|
|
|
|
|
_crt->output_sync(9 * crt_cycles_multiplier);
|
|
|
|
|
_outputPosition += 9;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// on lines prior to 28 or after or equal to 284, or on a line that is equal to 8 or 9 modulo 10 in a line-spaced mode,
|
|
|
|
|
// the line is then definitely blank.
|
|
|
|
|
if(current_line < 28 || current_line >= 284)
|
|
|
|
|
{
|
|
|
|
|
if(line_position == 9)
|
|
|
|
|
{
|
|
|
|
|
_crt->output_blank(119 * crt_cycles_multiplier);
|
2016-01-10 01:34:22 +00:00
|
|
|
|
_outputPosition = (_outputPosition + 119) % cycles_per_frame;;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// there are then 15 cycles of blank, 80 cycles of pixels, and 24 further cycles of blank
|
|
|
|
|
if(line_position == 9)
|
|
|
|
|
{
|
|
|
|
|
_crt->output_blank(15 * crt_cycles_multiplier);
|
|
|
|
|
_outputPosition += 15;
|
2016-01-10 03:19:00 +00:00
|
|
|
|
|
2016-01-10 03:39:46 +00:00
|
|
|
|
_crt->allocate_write_area(80 * crt_cycles_multiplier);
|
|
|
|
|
_currentLine = (uint8_t *)_crt->get_write_target_for_buffer(0);
|
|
|
|
|
|
|
|
|
|
if(current_line == 28)
|
|
|
|
|
_startLineAddress = _startScreenAddress;
|
|
|
|
|
_currentScreenAddress = _startLineAddress;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line_position >= 24 && line_position < 104)
|
|
|
|
|
{
|
2016-01-10 03:39:46 +00:00
|
|
|
|
if(_currentLine)
|
|
|
|
|
{
|
|
|
|
|
if(!(line_position&1))
|
|
|
|
|
{
|
|
|
|
|
uint8_t pixels = _ram[_currentScreenAddress];
|
|
|
|
|
_currentScreenAddress += 8;
|
|
|
|
|
int output_ptr = (line_position - 24) << 3;
|
|
|
|
|
|
|
|
|
|
for(int c = 0; c < 16; c+=2)
|
|
|
|
|
{
|
2016-01-10 03:52:08 +00:00
|
|
|
|
_currentLine[output_ptr + c] = _currentLine[output_ptr + c + 1] = (pixels&0x80) ? 0 : 7;
|
2016-01-10 03:39:46 +00:00
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_outputPosition++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line_position == 104)
|
|
|
|
|
{
|
2016-01-10 03:39:46 +00:00
|
|
|
|
if(!((current_line - 27)&7))
|
|
|
|
|
{
|
|
|
|
|
_startLineAddress += 40*8 - 7;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
_startLineAddress++;
|
|
|
|
|
|
|
|
|
|
_currentLine = nullptr;
|
|
|
|
|
_crt->output_data(80 * crt_cycles_multiplier);
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_crt->output_blank(24 * crt_cycles_multiplier);
|
|
|
|
|
_outputPosition += 24;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-10 02:32:56 +00:00
|
|
|
|
|
|
|
|
|
const char *Machine::get_signal_decoder()
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
"vec4 sample(vec2 coordinate)\n"
|
|
|
|
|
"{\n"
|
2016-01-10 03:19:00 +00:00
|
|
|
|
"float texValue = texture(texID, srcCoordinatesVarying).r;" // step(mod(texValue, 4.0), 2.0)
|
|
|
|
|
"return vec4( step(mod(texValue, 8.0/256.0), 4.0/256.0), step(mod(texValue, 4.0/256.0), 2.0/256.0), step(mod(texValue, 2.0/256.0), 1.0/256.0), 1.0);\n"
|
2016-01-10 02:32:56 +00:00
|
|
|
|
"}";
|
|
|
|
|
}
|