2016-06-05 14:51:07 +00:00
|
|
|
//
|
|
|
|
// 6560.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/06/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "6560.hpp"
|
|
|
|
|
|
|
|
using namespace MOS;
|
|
|
|
|
2016-06-05 21:06:10 +00:00
|
|
|
/*
|
|
|
|
0 - 0000 Black
|
|
|
|
1 - 0001 White
|
|
|
|
2 - 0010 Red
|
|
|
|
3 - 0011 Cyan
|
|
|
|
4 - 0100 Purple
|
|
|
|
5 - 0101 Green
|
|
|
|
6 - 0110 Blue
|
|
|
|
7 - 0111 Yellow
|
|
|
|
|
|
|
|
8 - 1000 Orange
|
|
|
|
9 - 1001 Light orange
|
|
|
|
10 - 1010 Pink
|
|
|
|
11 - 1011 Light cyan
|
|
|
|
12 - 1100 Light purple
|
|
|
|
13 - 1101 Light green
|
|
|
|
14 - 1110 Light blue
|
|
|
|
15 - 1111 Light yellow
|
|
|
|
*/
|
|
|
|
|
2016-06-07 01:56:02 +00:00
|
|
|
/*
|
2016-06-08 02:01:14 +00:00
|
|
|
0 -> purple
|
|
|
|
1 -> purple
|
2016-06-07 01:56:02 +00:00
|
|
|
2 -> browny yellow
|
|
|
|
3 -> browny red
|
|
|
|
4 -> purple
|
|
|
|
5 -> purple
|
|
|
|
6 -> cyan
|
|
|
|
7 -> green
|
|
|
|
8 -> green
|
|
|
|
*/
|
|
|
|
|
2016-06-05 14:51:07 +00:00
|
|
|
MOS6560::MOS6560() :
|
2016-06-12 18:39:17 +00:00
|
|
|
_crt(new Outputs::CRT::CRT(65*4, 4, 261, Outputs::CRT::ColourSpace::YUV, 228, 1, 1)), // TODO: turn 261 back into 263 once vertical sync exists
|
2016-06-05 21:06:10 +00:00
|
|
|
_horizontal_counter(0),
|
|
|
|
_vertical_counter(0)
|
2016-06-05 14:51:07 +00:00
|
|
|
{
|
2016-06-07 00:18:24 +00:00
|
|
|
_crt->set_composite_sampling_function(
|
2016-06-12 18:57:24 +00:00
|
|
|
"float angles[16] = float[]("
|
|
|
|
"0.0," // black
|
|
|
|
"0.0," // white
|
2016-06-12 18:39:17 +00:00
|
|
|
"1.963495408493621," // red
|
|
|
|
"5.105088062083414," // cyan
|
|
|
|
"0.785398163397448," // purple
|
|
|
|
"3.926990816987241," // green
|
|
|
|
"0.0," // blue
|
2016-06-12 18:57:24 +00:00
|
|
|
"3.141592653589793," // yellow
|
|
|
|
|
|
|
|
"2.356194490192345," // orange
|
|
|
|
"2.748893571891069," // light yellow
|
|
|
|
"1.963495408493621," // light red
|
|
|
|
"5.105088062083414," // light cyan
|
|
|
|
"0.785398163397448," // light purple
|
|
|
|
"3.926990816987241," // light green
|
|
|
|
"0.0," // light blue
|
|
|
|
"3.141592653589793" // light yellow
|
|
|
|
");"
|
|
|
|
"float brightnesses[16] = float[]("
|
|
|
|
"0.0 / 255.0,"
|
|
|
|
"255.0 / 255.0,"
|
|
|
|
"79.6875 / 255.0,"
|
|
|
|
"159.375 / 255.0,"
|
|
|
|
"95.625 / 255.0,"
|
|
|
|
"127.5 / 255.0,"
|
|
|
|
"63.75 / 255.0,"
|
|
|
|
"191.25 / 255.0,"
|
|
|
|
|
|
|
|
"95.625 / 255.0,"
|
|
|
|
"63.75 / 255.0,"
|
|
|
|
"127.5 / 255.0,"
|
|
|
|
"189.375 / 255.0,"
|
|
|
|
"135.625 / 255.0,"
|
|
|
|
"191.25 / 255.0,"
|
|
|
|
"119.53125 / 255.0,"
|
|
|
|
"159.375 / 255.0"
|
2016-06-12 18:39:17 +00:00
|
|
|
");"
|
2016-06-07 00:18:24 +00:00
|
|
|
"float composite_sample(usampler2D texID, vec2 coordinate, vec2 iCoordinate, float phase, float amplitude)"
|
2016-06-05 14:51:07 +00:00
|
|
|
"{"
|
2016-06-07 00:18:24 +00:00
|
|
|
"uint c = texture(texID, coordinate).r;"
|
2016-06-12 18:57:24 +00:00
|
|
|
"float y = brightnesses[c];"
|
|
|
|
"float phaseOffset = angles[c];"
|
2016-06-07 00:18:24 +00:00
|
|
|
|
2016-06-12 18:57:24 +00:00
|
|
|
// "float chroma = step(3.141592654, mod(phase + phaseOffset, 6.283185308)) * 2.0 - 1.0;"
|
|
|
|
"float chroma = cos(phase + phaseOffset);"
|
|
|
|
"return mix(y, chroma, amplitude);"
|
2016-06-05 14:51:07 +00:00
|
|
|
"}");
|
|
|
|
}
|
2016-06-05 16:11:12 +00:00
|
|
|
|
|
|
|
void MOS6560::set_register(int address, uint8_t value)
|
|
|
|
{
|
2016-06-09 02:15:24 +00:00
|
|
|
address &= 0xf;
|
2016-06-07 00:29:39 +00:00
|
|
|
_registers[address] = value;
|
2016-06-05 18:05:31 +00:00
|
|
|
switch(address)
|
|
|
|
{
|
|
|
|
case 0x0:
|
|
|
|
_interlaced = !!(value&0x80);
|
|
|
|
_first_column_location = value & 0x7f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x1:
|
|
|
|
_first_row_location = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x2:
|
|
|
|
_number_of_columns = value & 0x7f;
|
|
|
|
_video_matrix_start_address = (uint16_t)((_video_matrix_start_address & 0x3c00) | ((value & 0x80) << 2));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x3:
|
|
|
|
_number_of_rows = (value >> 1)&0x3f;
|
2016-06-12 15:23:57 +00:00
|
|
|
_tall_characters = !!(value&0x01);
|
2016-06-05 18:05:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x5:
|
|
|
|
_character_cell_start_address = (uint16_t)((value & 0x0f) << 10);
|
2016-06-10 22:12:21 +00:00
|
|
|
_video_matrix_start_address = (uint16_t)((_video_matrix_start_address & 0x0200) | ((value & 0xf0) << 6));
|
2016-06-05 18:05:31 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-07 00:18:24 +00:00
|
|
|
case 0xe:
|
|
|
|
_auxiliary_colour = value >> 4;
|
|
|
|
// TODO: sound amplitude
|
|
|
|
break;
|
|
|
|
|
2016-06-05 18:05:31 +00:00
|
|
|
case 0xf:
|
2016-06-05 22:02:49 +00:00
|
|
|
if(_this_state == State::Border)
|
|
|
|
{
|
|
|
|
output_border(_cycles_in_state * 4);
|
|
|
|
_cycles_in_state = 0;
|
|
|
|
}
|
2016-06-05 21:06:10 +00:00
|
|
|
_invertedCells = !!((value >> 3)&1);
|
|
|
|
_borderColour = value & 0x07;
|
|
|
|
_backgroundColour = value >> 4;
|
2016-06-05 18:05:31 +00:00
|
|
|
break;
|
|
|
|
|
2016-06-05 21:06:10 +00:00
|
|
|
// TODO: audio, primarily
|
|
|
|
|
2016-06-05 18:05:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-06-05 16:11:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 00:29:39 +00:00
|
|
|
uint8_t MOS6560::get_register(int address)
|
|
|
|
{
|
2016-06-09 02:15:24 +00:00
|
|
|
address &= 0xf;
|
|
|
|
switch(address)
|
|
|
|
{
|
|
|
|
default: return _registers[address];
|
2016-06-10 02:37:59 +00:00
|
|
|
case 0x03: return (uint8_t)(_vertical_counter << 7) | (_registers[3] & 0x7f);
|
|
|
|
case 0x04: return (_vertical_counter >> 1) & 0xff;
|
2016-06-09 02:15:24 +00:00
|
|
|
}
|
2016-06-07 00:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-05 22:02:49 +00:00
|
|
|
void MOS6560::output_border(unsigned int number_of_cycles)
|
|
|
|
{
|
|
|
|
uint8_t *colour_pointer = _crt->allocate_write_area(1);
|
|
|
|
if(colour_pointer) *colour_pointer = _borderColour;
|
|
|
|
_crt->output_level(number_of_cycles);
|
|
|
|
}
|
|
|
|
|
2016-06-05 16:11:12 +00:00
|
|
|
uint16_t MOS6560::get_address()
|
|
|
|
{
|
2016-06-05 21:06:10 +00:00
|
|
|
_horizontal_counter++;
|
|
|
|
if(_horizontal_counter == 65)
|
|
|
|
{
|
|
|
|
_horizontal_counter = 0;
|
|
|
|
_vertical_counter++;
|
|
|
|
_column_counter = -1;
|
|
|
|
|
|
|
|
if(_vertical_counter == 261)
|
|
|
|
{
|
|
|
|
_vertical_counter = 0;
|
|
|
|
_row_counter = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_row_counter >= 0)
|
|
|
|
{
|
|
|
|
_row_counter++;
|
2016-06-12 15:23:57 +00:00
|
|
|
if(_row_counter == _number_of_rows*(_tall_characters ? 16 : 8)) _row_counter = -1;
|
2016-06-05 21:06:10 +00:00
|
|
|
}
|
2016-06-07 00:33:06 +00:00
|
|
|
else if(_vertical_counter == _first_row_location * 2)
|
2016-06-06 11:35:35 +00:00
|
|
|
{
|
|
|
|
_video_matrix_line_address_counter = _video_matrix_start_address;
|
|
|
|
_row_counter = 0;
|
|
|
|
}
|
2016-06-05 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(_column_counter >= 0)
|
|
|
|
{
|
|
|
|
_column_counter++;
|
|
|
|
if(_column_counter == _number_of_columns*2)
|
2016-06-06 11:35:35 +00:00
|
|
|
{
|
2016-06-05 21:06:10 +00:00
|
|
|
_column_counter = -1;
|
2016-06-12 15:23:57 +00:00
|
|
|
if((_row_counter&(_tall_characters ? 15 : 7)) == (_tall_characters ? 15 : 7))
|
2016-06-06 11:35:35 +00:00
|
|
|
{
|
|
|
|
_video_matrix_line_address_counter = _video_matrix_address_counter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(_horizontal_counter == _first_column_location)
|
|
|
|
{
|
|
|
|
_column_counter = 0;
|
|
|
|
_video_matrix_address_counter = _video_matrix_line_address_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine output state; colour burst and sync timing are currently a guess
|
|
|
|
if(_horizontal_counter > 61) _this_state = State::ColourBurst;
|
|
|
|
else if(_horizontal_counter > 57) _this_state = State::Sync;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this_state = (_column_counter >= 0 && _row_counter >= 0) ? State::Pixels : State::Border;
|
2016-06-05 21:06:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 22:02:49 +00:00
|
|
|
// update the CRT
|
|
|
|
if(_this_state != _output_state)
|
|
|
|
{
|
|
|
|
switch(_output_state)
|
|
|
|
{
|
|
|
|
case State::Sync: _crt->output_sync(_cycles_in_state * 4); break;
|
|
|
|
case State::ColourBurst: _crt->output_colour_burst(_cycles_in_state * 4, 0, 0); break;
|
|
|
|
case State::Border: output_border(_cycles_in_state * 4); break;
|
|
|
|
case State::Pixels: _crt->output_data(_cycles_in_state * 4, 1); break;
|
|
|
|
}
|
|
|
|
_output_state = _this_state;
|
|
|
|
_cycles_in_state = 0;
|
|
|
|
|
|
|
|
pixel_pointer = nullptr;
|
|
|
|
if(_output_state == State::Pixels)
|
|
|
|
{
|
|
|
|
pixel_pointer = _crt->allocate_write_area(260);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_cycles_in_state++;
|
|
|
|
|
|
|
|
// compute the address
|
|
|
|
if(_this_state == State::Pixels)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Per http://tinyvga.com/6561 :
|
|
|
|
|
|
|
|
The basic video timing is very simple. For
|
|
|
|
every character the VIC-I is about to display, it first fetches the
|
|
|
|
character code and colour, then the character appearance (from the
|
|
|
|
character generator memory). The character codes are read on every
|
|
|
|
raster line, thus making every line a "bad line". When the raster
|
|
|
|
beam is outside of the text window, the videochip reads from $001c for
|
|
|
|
most time. (Some videochips read from $181c instead.) The address
|
|
|
|
occasionally varies, but it might also be due to a flaky bus. (By
|
|
|
|
reading from unconnected address space, such as $9100-$910f, you can
|
|
|
|
read the data fetched by the videochip on the previous clock cycle.)
|
|
|
|
*/
|
|
|
|
if(_column_counter&1)
|
2016-06-06 11:35:35 +00:00
|
|
|
{
|
2016-06-12 15:23:57 +00:00
|
|
|
return _character_cell_start_address + (_character_code*(_tall_characters ? 16 : 8)) + (_row_counter&(_tall_characters ? 15 : 7));
|
2016-06-06 11:35:35 +00:00
|
|
|
}
|
2016-06-05 22:02:49 +00:00
|
|
|
else
|
2016-06-06 11:35:35 +00:00
|
|
|
{
|
|
|
|
uint16_t result = _video_matrix_address_counter;
|
|
|
|
_video_matrix_address_counter++;
|
|
|
|
return result;
|
|
|
|
}
|
2016-06-05 22:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 18:05:31 +00:00
|
|
|
return 0x1c;
|
2016-06-05 16:11:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 20:28:06 +00:00
|
|
|
void MOS6560::set_graphics_value(uint8_t value, uint8_t colour_value)
|
2016-06-05 16:11:12 +00:00
|
|
|
{
|
2016-06-06 11:47:30 +00:00
|
|
|
// TODO: this isn't correct, as _character_value will be
|
|
|
|
// accessed second, then output will roll over. Probably it's
|
2016-06-07 00:18:24 +00:00
|
|
|
// correct (given the delays upstream) to output all 8 on an &1
|
2016-06-06 11:47:30 +00:00
|
|
|
// and to adjust the signalling to the CRT above?
|
2016-06-05 22:02:49 +00:00
|
|
|
if(_this_state == State::Pixels)
|
|
|
|
{
|
|
|
|
if(_column_counter&1)
|
|
|
|
{
|
2016-06-06 11:35:35 +00:00
|
|
|
_character_value = value;
|
|
|
|
|
2016-06-05 22:02:49 +00:00
|
|
|
if(pixel_pointer)
|
|
|
|
{
|
2016-06-07 00:18:24 +00:00
|
|
|
uint8_t cell_colour = _character_colour & 0x7;
|
|
|
|
if(!(_character_colour&0x8))
|
|
|
|
{
|
|
|
|
pixel_pointer[0] = ((_character_value >> 7)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[1] = ((_character_value >> 6)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[2] = ((_character_value >> 5)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[3] = ((_character_value >> 4)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[4] = ((_character_value >> 3)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[5] = ((_character_value >> 2)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[6] = ((_character_value >> 1)&1) ? cell_colour : _backgroundColour;
|
|
|
|
pixel_pointer[7] = ((_character_value >> 0)&1) ? cell_colour : _backgroundColour;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t colours[4] = {_backgroundColour, _borderColour, cell_colour, _auxiliary_colour};
|
|
|
|
pixel_pointer[0] =
|
|
|
|
pixel_pointer[1] = colours[(_character_value >> 6)&3];
|
|
|
|
pixel_pointer[2] =
|
|
|
|
pixel_pointer[3] = colours[(_character_value >> 4)&3];
|
|
|
|
pixel_pointer[4] =
|
|
|
|
pixel_pointer[5] = colours[(_character_value >> 2)&3];
|
|
|
|
pixel_pointer[6] =
|
|
|
|
pixel_pointer[7] = colours[(_character_value >> 0)&3];
|
|
|
|
}
|
|
|
|
pixel_pointer += 8;
|
2016-06-05 22:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_character_code = value;
|
|
|
|
_character_colour = colour_value;
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 16:11:12 +00:00
|
|
|
}
|