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),
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition(0),
|
|
|
|
|
_audioOutputPosition(0),
|
|
|
|
|
_audioOutputPositionError(0),
|
2016-01-12 03:08:30 +00:00
|
|
|
|
_currentOutputLine(0)
|
2016-01-05 04:12:47 +00:00
|
|
|
|
{
|
2016-01-12 00:48:31 +00:00
|
|
|
|
memset(_keyStates, 0, sizeof(_keyStates));
|
2016-01-12 03:08:30 +00:00
|
|
|
|
memset(_palette, 0xf, sizeof(_palette));
|
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-13 03:34:26 +00:00
|
|
|
|
for(int c = 0; c < 16; c++)
|
|
|
|
|
memset(_roms[c], 0xff, 16384);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
|
2016-01-14 03:02:39 +00:00
|
|
|
|
_speaker.set_input_rate(125000);
|
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-12 03:29:16 +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.
|
2016-01-10 01:26:40 +00:00
|
|
|
|
update_display();
|
2016-01-12 03:29:16 +00:00
|
|
|
|
|
|
|
|
|
_ram[address] = *value;
|
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-13 03:34:26 +00:00
|
|
|
|
if(_screenMode < 4)
|
|
|
|
|
{
|
|
|
|
|
const int current_line = _frameCycles >> 7;
|
|
|
|
|
const int line_position = _frameCycles & 127;
|
|
|
|
|
if(current_line >= 28 && current_line < 28+256 && line_position >= 24 && line_position < 104)
|
|
|
|
|
cycles = (unsigned int)(104 - line_position);
|
|
|
|
|
}
|
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);
|
|
|
|
|
// }
|
2016-01-10 22:17:39 +00:00
|
|
|
|
if(((_activeRom&12) != 8) || (nextROM&8))
|
2016-01-10 17:30:24 +00:00
|
|
|
|
{
|
|
|
|
|
_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:
|
2016-01-14 02:03:43 +00:00
|
|
|
|
if(!isReadOperation(operation))
|
|
|
|
|
{
|
2016-01-14 03:11:33 +00:00
|
|
|
|
update_audio();
|
|
|
|
|
_speaker.set_divider(*value);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
}
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0x7:
|
2016-01-12 03:08:30 +00:00
|
|
|
|
if(!isReadOperation(operation))
|
|
|
|
|
{
|
2016-01-14 02:03:43 +00:00
|
|
|
|
// update screen mode
|
|
|
|
|
uint8_t new_screen_mode = ((*value) >> 3)&7;
|
|
|
|
|
if(new_screen_mode == 7) new_screen_mode = 4;
|
|
|
|
|
if(new_screen_mode != _screenMode)
|
|
|
|
|
{
|
|
|
|
|
update_display();
|
|
|
|
|
_screenMode = new_screen_mode;
|
|
|
|
|
switch(_screenMode)
|
|
|
|
|
{
|
|
|
|
|
case 0: case 1: case 2: _screenModeBaseAddress = 0x3000; break;
|
|
|
|
|
case 3: _screenModeBaseAddress = 0x4000; break;
|
|
|
|
|
case 4: case 5: _screenModeBaseAddress = 0x5800; break;
|
|
|
|
|
case 6: _screenModeBaseAddress = 0x6000; break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update speaker mode
|
|
|
|
|
bool new_speaker_is_enabled = (*value & 6) == 2;
|
2016-01-14 03:11:33 +00:00
|
|
|
|
if(new_speaker_is_enabled != _speaker.get_is_enabled())
|
2016-01-12 03:08:30 +00:00
|
|
|
|
{
|
2016-01-14 02:03:43 +00:00
|
|
|
|
update_audio();
|
2016-01-14 03:11:33 +00:00
|
|
|
|
_speaker.set_is_enabled(new_speaker_is_enabled);
|
2016-01-12 03:08:30 +00:00
|
|
|
|
}
|
2016-01-14 02:03:43 +00:00
|
|
|
|
|
|
|
|
|
// TODO: tape mode, tape motor, caps lock LED
|
2016-01-12 03:08:30 +00:00
|
|
|
|
}
|
2016-01-08 01:36:27 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-01-12 03:08:30 +00:00
|
|
|
|
{
|
|
|
|
|
if(!isReadOperation(operation))
|
|
|
|
|
{
|
|
|
|
|
update_display();
|
|
|
|
|
|
|
|
|
|
static const int registers[4][4] = {
|
|
|
|
|
{10, 8, 2, 0},
|
|
|
|
|
{14, 12, 6, 4},
|
|
|
|
|
{15, 13, 7, 5},
|
|
|
|
|
{11, 9, 3, 1},
|
|
|
|
|
};
|
|
|
|
|
const int index = (address >> 1)&3;
|
|
|
|
|
const uint8_t colour = ~(*value);
|
|
|
|
|
if(address&1)
|
|
|
|
|
{
|
|
|
|
|
_palette[registers[index][0]] = (_palette[registers[index][0]]&3) | ((colour >> 1)&4);
|
|
|
|
|
_palette[registers[index][1]] = (_palette[registers[index][1]]&3) | ((colour >> 0)&4);
|
|
|
|
|
_palette[registers[index][2]] = (_palette[registers[index][2]]&3) | ((colour << 1)&4);
|
|
|
|
|
_palette[registers[index][3]] = (_palette[registers[index][3]]&3) | ((colour << 2)&4);
|
|
|
|
|
|
|
|
|
|
_palette[registers[index][2]] = (_palette[registers[index][2]]&5) | ((colour >> 4)&2);
|
|
|
|
|
_palette[registers[index][3]] = (_palette[registers[index][3]]&5) | ((colour >> 3)&2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_palette[registers[index][0]] = (_palette[registers[index][0]]&6) | ((colour >> 7)&1);
|
|
|
|
|
_palette[registers[index][1]] = (_palette[registers[index][1]]&6) | ((colour >> 6)&1);
|
|
|
|
|
_palette[registers[index][2]] = (_palette[registers[index][2]]&6) | ((colour >> 5)&1);
|
|
|
|
|
_palette[registers[index][3]] = (_palette[registers[index][3]]&6) | ((colour >> 4)&1);
|
|
|
|
|
|
|
|
|
|
_palette[registers[index][0]] = (_palette[registers[index][0]]&5) | ((colour >> 2)&2);
|
|
|
|
|
_palette[registers[index][1]] = (_palette[registers[index][1]]&5) | ((colour >> 1)&2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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 ROMSlotKeyboard:
|
|
|
|
|
case ROMSlotKeyboard+1:
|
2016-01-10 17:30:24 +00:00
|
|
|
|
*value = 0xf0;
|
2016-01-12 00:48:31 +00:00
|
|
|
|
for(int address_line = 0; address_line < 14; address_line++)
|
|
|
|
|
{
|
|
|
|
|
if(!(address&(1 << address_line))) *value |= _keyStates[address_line];
|
|
|
|
|
}
|
2016-01-10 17:30:24 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-01-13 03:34:26 +00:00
|
|
|
|
*value = _roms[_activeRom][address & 16383];
|
2016-01-08 02:01:13 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-07 02:09:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 00:06:46 +00:00
|
|
|
|
// if(operation == CPU6502::BusOperation::ReadOpcode)
|
|
|
|
|
// {
|
|
|
|
|
// printf("%04x: %02x (%d)\n", address, *value, _frameCycles);
|
|
|
|
|
// }
|
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
_frameCycles += cycles;
|
2016-01-14 02:03:43 +00:00
|
|
|
|
switch(_frameCycles)
|
2016-01-10 01:26:40 +00:00
|
|
|
|
{
|
2016-01-14 02:03:43 +00:00
|
|
|
|
case 64*128:
|
|
|
|
|
update_audio();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 128*128:
|
|
|
|
|
update_audio();
|
|
|
|
|
signal_interrupt(InterruptRealTimeClock);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 196*128:
|
|
|
|
|
update_audio();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 284*128:
|
|
|
|
|
update_audio();
|
|
|
|
|
signal_interrupt(InterruptDisplayEnd);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case cycles_per_frame:
|
|
|
|
|
update_display();
|
|
|
|
|
update_audio();
|
|
|
|
|
_frameCycles = 0;
|
|
|
|
|
_displayOutputPosition = 0;
|
|
|
|
|
_audioOutputPosition = 0;
|
|
|
|
|
_currentOutputLine = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-13 03:34:26 +00:00
|
|
|
|
case ROMSlotOS: target = _os; break;
|
|
|
|
|
default: target = _roms[slot]; break;
|
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)
|
|
|
|
|
{
|
2016-01-10 22:17:39 +00:00
|
|
|
|
_interruptStatus |= interrupt;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
evaluate_interrupts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void Machine::evaluate_interrupts()
|
|
|
|
|
{
|
|
|
|
|
if(_interruptStatus & _interruptControl)
|
|
|
|
|
{
|
|
|
|
|
_interruptStatus |= 1;
|
|
|
|
|
}
|
2016-01-11 00:06:46 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_interruptStatus &= ~1;
|
|
|
|
|
}
|
2016-01-10 01:26:40 +00:00
|
|
|
|
set_irq_line(_interruptStatus & 1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 02:03:43 +00:00
|
|
|
|
inline void Machine::update_audio()
|
|
|
|
|
{
|
|
|
|
|
int difference = _frameCycles - _audioOutputPosition;
|
|
|
|
|
_audioOutputPosition = _frameCycles;
|
2016-01-14 03:02:39 +00:00
|
|
|
|
_speaker.run_for_cycles((_audioOutputPositionError + difference) >> 4);
|
|
|
|
|
_audioOutputPositionError = (_audioOutputPositionError + difference)&15;
|
2016-01-14 02:03:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-10 01:26:40 +00:00
|
|
|
|
inline void Machine::update_display()
|
|
|
|
|
{
|
2016-01-12 03:18:34 +00:00
|
|
|
|
const int lines_of_hsync = 3;
|
|
|
|
|
const int end_of_hsync = lines_of_hsync * cycles_per_line;
|
|
|
|
|
const int first_graphics_line = 28;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
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-14 02:03:43 +00:00
|
|
|
|
if(_displayOutputPosition < end_of_hsync)
|
2016-01-10 01:26:40 +00:00
|
|
|
|
{
|
2016-01-12 03:18:34 +00:00
|
|
|
|
for(int c = 0; c < lines_of_hsync; c++)
|
2016-01-10 02:53:33 +00:00
|
|
|
|
{
|
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-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition = end_of_hsync;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-14 02:03:43 +00:00
|
|
|
|
while(_displayOutputPosition >= end_of_hsync && _displayOutputPosition < _frameCycles)
|
2016-01-10 01:26:40 +00:00
|
|
|
|
{
|
2016-01-14 02:03:43 +00:00
|
|
|
|
const int current_line = _displayOutputPosition >> 7;
|
|
|
|
|
const int line_position = _displayOutputPosition & 127;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
|
|
|
|
|
// all lines then start with 9 cycles of sync
|
|
|
|
|
if(!line_position)
|
|
|
|
|
{
|
|
|
|
|
_crt->output_sync(9 * crt_cycles_multiplier);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition += 9;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
bool isBlankLine =
|
|
|
|
|
((_screenMode == 3) || (_screenMode == 6)) ?
|
2016-01-12 03:18:34 +00:00
|
|
|
|
((current_line < first_graphics_line || current_line >= first_graphics_line+248) || (((current_line - first_graphics_line)%10) > 7)) :
|
|
|
|
|
((current_line < first_graphics_line || current_line >= first_graphics_line+256));
|
2016-01-12 03:08:30 +00:00
|
|
|
|
|
|
|
|
|
if(isBlankLine)
|
2016-01-10 01:26:40 +00:00
|
|
|
|
{
|
|
|
|
|
if(line_position == 9)
|
|
|
|
|
{
|
|
|
|
|
_crt->output_blank(119 * crt_cycles_multiplier);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition += 119;
|
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);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition += 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);
|
|
|
|
|
|
2016-01-12 03:18:34 +00:00
|
|
|
|
if(current_line == first_graphics_line)
|
2016-01-10 03:39:46 +00:00
|
|
|
|
_startLineAddress = _startScreenAddress;
|
|
|
|
|
_currentScreenAddress = _startLineAddress;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line_position >= 24 && line_position < 104)
|
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
if(_currentLine && ((_screenMode < 4) || !(line_position&1)))
|
2016-01-10 03:39:46 +00:00
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
if(_currentScreenAddress&32768)
|
2016-01-10 03:39:46 +00:00
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
_currentScreenAddress = _screenModeBaseAddress + (_currentScreenAddress&32767);
|
|
|
|
|
}
|
|
|
|
|
uint8_t pixels = _ram[_currentScreenAddress];
|
|
|
|
|
_currentScreenAddress = _currentScreenAddress+8;
|
|
|
|
|
int output_ptr = (line_position - 24) << 3;
|
2016-01-10 03:39:46 +00:00
|
|
|
|
|
2016-01-12 03:08:30 +00:00
|
|
|
|
switch(_screenMode)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
case 3:
|
|
|
|
|
for(int c = 0; c < 8; c++)
|
|
|
|
|
{
|
|
|
|
|
uint8_t colour = (pixels&0x80) >> 4;
|
|
|
|
|
_currentLine[output_ptr + c] = _palette[colour];
|
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
for(int c = 0; c < 8; c += 2)
|
|
|
|
|
{
|
|
|
|
|
uint8_t colour = ((pixels&0x80) >> 4) | ((pixels&0x08) >> 2);
|
|
|
|
|
_currentLine[output_ptr + c + 0] = _currentLine[output_ptr + c + 1] = _palette[colour];
|
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
for(int c = 0; c < 8; c += 4)
|
|
|
|
|
{
|
|
|
|
|
uint8_t colour = ((pixels&0x80) >> 4) | ((pixels&0x20) >> 3) | ((pixels&0x08) >> 2) | ((pixels&0x02) >> 1);
|
|
|
|
|
_currentLine[output_ptr + c + 0] = _currentLine[output_ptr + c + 1] =
|
|
|
|
|
_currentLine[output_ptr + c + 2] = _currentLine[output_ptr + c + 3] = _palette[colour];
|
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
|
for(int c = 0; c < 16; c += 4)
|
|
|
|
|
{
|
|
|
|
|
uint8_t colour = ((pixels&0x80) >> 4) | ((pixels&0x08) >> 2);
|
|
|
|
|
_currentLine[output_ptr + c + 0] = _currentLine[output_ptr + c + 1] =
|
|
|
|
|
_currentLine[output_ptr + c + 2] = _currentLine[output_ptr + c + 3] = _palette[colour];
|
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
case 4:
|
|
|
|
|
case 6:
|
|
|
|
|
for(int c = 0; c < 16; c += 2)
|
|
|
|
|
{
|
|
|
|
|
uint8_t colour = (pixels&0x80) >> 4;
|
|
|
|
|
_currentLine[output_ptr + c] = _currentLine[output_ptr + c + 1] = _palette[colour];
|
|
|
|
|
pixels <<= 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-01-10 03:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition++;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(line_position == 104)
|
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
_currentOutputLine++;
|
|
|
|
|
if(!(_currentOutputLine&7))
|
2016-01-10 03:39:46 +00:00
|
|
|
|
{
|
2016-01-12 03:08:30 +00:00
|
|
|
|
_startLineAddress += ((_screenMode < 4) ? 80 : 40)*8 - 7;
|
2016-01-10 03:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
2016-01-14 02:03:43 +00:00
|
|
|
|
_displayOutputPosition += 24;
|
2016-01-10 01:26:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-10 02:32:56 +00:00
|
|
|
|
|
|
|
|
|
const char *Machine::get_signal_decoder()
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
"vec4 sample(vec2 coordinate)\n"
|
|
|
|
|
"{\n"
|
2016-01-12 21:27:09 +00:00
|
|
|
|
"float texValue = texture(texID, coordinate).r;\n"
|
2016-01-12 03:08:30 +00:00
|
|
|
|
"return vec4( step(4.0/256.0, mod(texValue, 8.0/256.0)), step(2.0/256.0, mod(texValue, 4.0/256.0)), step(1.0/256.0, mod(texValue, 2.0/256.0)), 1.0);\n"
|
2016-01-10 02:32:56 +00:00
|
|
|
|
"}";
|
|
|
|
|
}
|
2016-01-12 00:48:31 +00:00
|
|
|
|
|
|
|
|
|
void Machine::set_key_state(Key key, bool isPressed)
|
|
|
|
|
{
|
|
|
|
|
if(key == KeyBreak)
|
|
|
|
|
{
|
|
|
|
|
set_reset_line(isPressed);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(isPressed)
|
|
|
|
|
_keyStates[key >> 4] |= key&0xf;
|
|
|
|
|
else
|
|
|
|
|
_keyStates[key >> 4] &= ~(key&0xf);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-14 03:02:39 +00:00
|
|
|
|
|
2016-01-15 02:27:02 +00:00
|
|
|
|
void Machine::Speaker::get_samples(unsigned int number_of_samples, int16_t *target)
|
2016-01-14 03:02:39 +00:00
|
|
|
|
{
|
2016-01-15 02:33:27 +00:00
|
|
|
|
if(!_is_enabled)
|
|
|
|
|
{
|
|
|
|
|
*target = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-01-14 03:11:33 +00:00
|
|
|
|
{
|
2016-01-15 02:27:02 +00:00
|
|
|
|
*target = _output_level;
|
2016-01-15 02:59:37 +00:00
|
|
|
|
// fwrite(target, sizeof(int16_t), 1, rawStream);
|
2016-01-14 03:11:33 +00:00
|
|
|
|
}
|
2016-01-15 02:27:02 +00:00
|
|
|
|
skip_samples(number_of_samples);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Machine::Speaker::skip_samples(unsigned int number_of_samples)
|
|
|
|
|
{
|
|
|
|
|
while(number_of_samples--)
|
2016-01-14 03:11:33 +00:00
|
|
|
|
{
|
2016-01-15 02:27:02 +00:00
|
|
|
|
_counter ++;
|
|
|
|
|
if(_counter > _divider)
|
|
|
|
|
{
|
|
|
|
|
_counter = 0;
|
2016-01-15 02:33:27 +00:00
|
|
|
|
_output_level ^= 8192;
|
2016-01-15 02:27:02 +00:00
|
|
|
|
}
|
2016-01-14 03:11:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Machine::Speaker::set_divider(uint8_t divider)
|
|
|
|
|
{
|
2016-01-15 02:33:27 +00:00
|
|
|
|
_divider = divider;
|
2016-01-14 03:11:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Machine::Speaker::set_is_enabled(bool is_enabled)
|
|
|
|
|
{
|
2016-01-15 02:33:27 +00:00
|
|
|
|
_is_enabled = is_enabled;
|
|
|
|
|
_counter = 0;
|
2016-01-14 03:02:39 +00:00
|
|
|
|
}
|
2016-01-15 02:59:37 +00:00
|
|
|
|
|
|
|
|
|
Machine::Speaker::Speaker() : _counter(0), _divider(0x32), _is_enabled(false), _output_level(0)
|
|
|
|
|
{
|
|
|
|
|
// rawStream = fopen("/Users/thomasharte/Desktop/sound.rom", "wb");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Machine::Speaker::~Speaker()
|
|
|
|
|
{
|
|
|
|
|
// fclose(rawStream);
|
|
|
|
|
}
|