Skip to content

Commit

Permalink
wip property rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gulrak committed Oct 8, 2024
1 parent fb2ee16 commit da5bf60
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 32 deletions.
44 changes: 29 additions & 15 deletions src/cadmium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,10 +1101,12 @@ class Cadmium : public emu::EmuHostEx
(Vector2){0, 0}, 0.0f, WHITE);
#else
if (_scaleBy2) {
drawScreen({_screenOverlay.x * 2, _screenOverlay.y * 2, _screenOverlay.width * 2, _screenOverlay.height * 2}, _screenScale);
DrawTexturePro(_renderTexture.texture, (Rectangle){0, 0, (float)_renderTexture.texture.width, -(float)_renderTexture.texture.height}, (Rectangle){0, 0, (float)_renderTexture.texture.width * 2, (float)_renderTexture.texture.height * 2},
(Vector2){0, 0}, 0.0f, WHITE);
}
else {
drawScreen(_screenOverlay, _screenScale);
DrawTextureRec(_renderTexture.texture, (Rectangle){0, 0, (float)_renderTexture.texture.width, -(float)_renderTexture.texture.height}, (Vector2){0, 0}, WHITE);
}
#endif
Expand All @@ -1130,24 +1132,23 @@ class Cadmium : public emu::EmuHostEx
void drawScreen(Rectangle dest, int gridScale)
{
const Color gridLineCol{40,40,40,255};
bool crt = _renderCrt;
int scrWidth = crt ? 130 : _chipEmu->getCurrentScreenWidth();
int scrWidth = _chipEmu->getCurrentScreenWidth();
//int scrHeight = crt ? 385 : (_chipEmu->isGenericEmulation() ? _chipEmu->getCurrentScreenHeight() : 128);
int scrHeight = crt ? 385 : _chipEmu->getCurrentScreenHeight();
int scrHeight = _chipEmu->getCurrentScreenHeight();
auto videoScaleX = dest.width / scrWidth;
auto videoScaleY = _chipEmu->isGenericEmulation() ? videoScaleX : videoScaleX/4;
auto videoX = crt ? (dest.width - scrWidth * videoScaleX) / 2 + dest.x : (dest.width - _chipEmu->getCurrentScreenWidth() * videoScaleX) / 2 + dest.x;
auto videoY = crt ? (dest.height - scrHeight * videoScaleY) / 2 + dest.y : (dest.height - _chipEmu->getCurrentScreenHeight() * videoScaleY) / 2 + dest.y;
auto videoScaleY = _chipEmu->getScreen() && _chipEmu->getScreen()->ratio() ? videoScaleX / _chipEmu->getScreen()->ratio() : videoScaleX;
auto videoX = (dest.width - _chipEmu->getCurrentScreenWidth() * videoScaleX) / 2 + dest.x;
auto videoY = (dest.height - _chipEmu->getCurrentScreenHeight() * videoScaleY) / 2 + dest.y;
if(_chipEmu->getMaxScreenWidth() > 128)
DrawRectangleRec(dest, {0,0,0,255});
else
DrawRectangleRec(dest, {0,12,24,255});
if(crt)
DrawTexturePro(_crtTexture, {1, 1, (float)scrWidth-2, (float)scrHeight-2}, {videoX, videoY, scrWidth * videoScaleX, scrHeight * videoScaleY}, {0, 0}, 0, WHITE);
else
DrawTexturePro(_screenTexture, {0, 0, (float)scrWidth, (float)scrHeight}, {videoX, videoY, scrWidth * videoScaleX, scrHeight * videoScaleY}, {0, 0}, 0, WHITE);
// DrawRectangleLines(videoX, videoY, scrWidth * videoScale, scrHeight * videoScaleY, RED);
if (_grid && !crt) {

//BeginShaderMode(_scanLineShader);
DrawTexturePro(_screenTexture, {0, 0, (float)scrWidth, (float)scrHeight}, {videoX, videoY, scrWidth * videoScaleX, scrHeight * videoScaleY}, {0, 0}, 0, WHITE);
//EndShaderMode();

if (_grid) {
for (short x = 0; x < scrWidth; ++x) {
DrawRectangle(videoX + x * gridScale, videoY, 1, scrHeight * videoScaleY, gridLineCol);
}
Expand Down Expand Up @@ -1491,14 +1492,25 @@ class Cadmium : public emu::EmuHostEx
switch (_mainView) {
case eDEBUGGER: {
_lastView = _lastRunView = _mainView;
_debugger.render(_font, [this](Rectangle video, int scale) { drawScreen(video, scale); });
_debugger.render(_font, [this](Rectangle video, int scale) {
_screenOverlay = video;
_screenScale = scale;
rlSetBlendFactors(1, 0, 0x8006);
rlSetBlendMode(RL_BLEND_CUSTOM);
DrawRectangleRec(_screenOverlay, {0,0,0,0});
rlSetBlendMode(RL_BLEND_ALPHA);
});
break;
}
case eVIDEO: {
_lastView = _lastRunView = _mainView;
gridScale = _screenWidth / _chipEmu->getCurrentScreenWidth();
video = {0, 20, (float)_screenWidth, (float)_screenHeight - 36};
drawScreen(video, gridScale);
_screenOverlay = {0, 20, (float)_screenWidth, (float)_screenHeight - 36};
_screenScale = gridScale;
rlSetBlendFactors(1, 0, 0x8006);
rlSetBlendMode(RL_BLEND_CUSTOM);
DrawRectangleRec(_screenOverlay, {0,0,0,0});
rlSetBlendMode(RL_BLEND_ALPHA);
break;
}
case eEDITOR:
Expand Down Expand Up @@ -2433,6 +2445,8 @@ class Cadmium : public emu::EmuHostEx
uint32_t* _selectedColor{nullptr};
std::string _colorText;
uint32_t _previousColor{};
Rectangle _screenOverlay{};
int _screenScale{1};

//std::string _romName;
//std::vector<uint8_t> _romImage;
Expand Down
26 changes: 23 additions & 3 deletions src/emulation/chip8generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ struct C8GenericFactoryInfo final : public CoreRegistry::FactoryInfo<Chip8Generi

static bool registeredHleC8 = CoreRegistry::registerFactory(PROP_CLASS, std::make_unique<C8GenericFactoryInfo>("Default HLE CHIP-8 emulation"));

static constexpr uint32_t cdp1862BackgroundColors[4] = { 0x000080FF, 0x000000FF, 0x008000FF, 0x800000FF };

Chip8GenericEmulator::Chip8GenericEmulator(EmulatorHost& host, Properties& props, IChip8Emulator* other)
: Chip8GenericBase(Chip8GenericOptions::fromProperties(props).variant(), {})
Expand All @@ -335,6 +336,7 @@ Chip8GenericEmulator::Chip8GenericEmulator(EmulatorHost& host, Properties& props
_screen.setPalette(props.palette());
}
setHandler();
Chip8GenericEmulator::reset();
}

GenericCpu::StackContent Chip8GenericEmulator::stack() const
Expand Down Expand Up @@ -437,9 +439,27 @@ void Chip8GenericEmulator::reset()
_sampleStep = 0;
_mcSamplePos = 0;
_blendMode = eBLEND_NORMAL;
_mcPalette.fill(0x00);
_mcPalette[1] = 0xffffffff;
_mcPalette[254] = 0xffffffff;
_simpleRandState = _simpleRandSeed;
if(_options.behaviorBase == Chip8GenericOptions::eCHIP8X) {
static uint32_t foregroundColors[8] = { 0x181818FF, 0xFF0000FF, 0x0000FFFF, 0xFF00FFFF, 0x00FF00FF, 0xFFFF00FF, 0x00FFFFFF, 0xFFFFFFFF };
_screen.setMode(256, 192, 4); // actual resolution doesn't matter, just needs to be bigger than max resolution, but ratio matters
_screen.setOverlayCellHeight(-1); // reset
_chip8xBackgroundColor = 0;
for(int i = 0; i < 256; ++i) {
if(i & 0xF) {
_mcPalette[i] = foregroundColors[(i>>4) & 7];
}
else {
_mcPalette[i] = cdp1862BackgroundColors[0];
}
}
_screen.setPalette(_mcPalette);
}
else if(_options.behaviorBase == Chip8GenericOptions::eMEGACHIP) {
_mcPalette.fill(0x00);
_mcPalette[1] = 0xffffffff;
_mcPalette[254] = 0xffffffff;
}
}

bool Chip8GenericEmulator::updateProperties(Properties& props, Property& changed)
Expand Down
12 changes: 6 additions & 6 deletions src/emulation/cosmacvip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static CosmacVipSetupInfo vipPresets[] = {
"128x64 CHIP-8 with hardware modifications, from #VIPER-V1-I7 and #IpsoFacto-I10, by Ben H. Hutchinson, Jr., 1979",
".ch10;.c10",
chip8::Variant::CHIP_10,
{ .cpuType = "CDP1802", .clockFrequency = 1760640, .ramSize = 4096, .cleanRam = true, .traceLog = false, .videoType = VVT_CDP1861, .audioType = VAT_CA555_BUZZER, .keyboard = VIPK_HEX, .romName = "COSMAC-VIP", .interpreter = VC8I_CHIP10, .startAddress = 512}
{ .cpuType = "CDP1802", .clockFrequency = 1760640, .ramSize = 4096, .cleanRam = true, .traceLog = false, .videoType = VVT_CDP1861_C10_HIRES, .audioType = VAT_CA555_BUZZER, .keyboard = VIPK_HEX, .romName = "COSMAC-VIP", .interpreter = VC8I_CHIP10, .startAddress = 512}
},
{
"CHIP-8 RB",
Expand Down Expand Up @@ -215,7 +215,7 @@ class CosmacVIP::Private {
, _properties(properties)
, _options(CosmacVIPOptions::fromProperties(properties))
, _cpu(bus, CPU_CLOCK_FREQUENCY)
, _video(_options.videoType == VVT_CDP1861 ? Cdp186x::eCDP1861 : Cdp186x::eVP590, _cpu, false)
, _video(_options.videoType == VVT_CDP1861 ? Cdp186x::eCDP1861 : _options.videoType == VVT_CDP1861_C10_HIRES ? Cdp186x::eCDP1861_C10 : Cdp186x::eVP590, _cpu, false)
{
using namespace std::string_literals;
(void)registeredVIP;
Expand Down Expand Up @@ -1099,22 +1099,22 @@ void CosmacVIP::renderAudio(int16_t* samples, size_t frames, int sampleFrequency

uint16_t CosmacVIP::getCurrentScreenWidth() const
{
return 64;
return _impl->_options.videoType == VVT_CDP1861_C10_HIRES ? 128 : 64;
}

uint16_t CosmacVIP::getCurrentScreenHeight() const
{
return 128;
return _impl->_options.videoType == VVT_CDP1861_C10_HIRES ? 64 : 128;
}

uint16_t CosmacVIP::getMaxScreenWidth() const
{
return 64;
return _impl->_options.videoType == VVT_CDP1861_C10_HIRES ? 128 : 64;
}

uint16_t CosmacVIP::getMaxScreenHeight() const
{
return 128;
return _impl->_options.videoType == VVT_CDP1861_C10_HIRES ? 64 : 128;
}

const VideoType* CosmacVIP::getScreen() const
Expand Down
13 changes: 11 additions & 2 deletions src/emulation/hardware/cdp186x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Cdp186x::Cdp186x(Type type, Cdp1802& cpu, bool traceLog)
, _traceLog(traceLog)
{
static uint32_t foregroundColors[8] = { 0x181818FF, 0xFF0000FF, 0x0000FFFF, 0xFF00FFFF, 0x00FF00FF, 0xFFFF00FF, 0x00FFFFFF, 0xFFFFFFFF };
_screen.setMode(256, 192, 4); // actual resolution doesn't matter, just needs to be bigger than max resolution, but ratio matters
_screen.setMode(256, 192, _type == eCDP1861_C10 ? 1 : 4); // actual resolution doesn't matter, just needs to be bigger than max resolution, but ratio matters
for(int i = 0; i < 256; ++i) {
if(i & 0xF) {
_cdp1862Palette[i] = foregroundColors[(i>>4) & 7];
Expand Down Expand Up @@ -128,8 +128,17 @@ std::pair<int,bool> Cdp186x::executeStep()
if(mask)
highBits = _cpu.readByteDMA(0xD000 | (addr & mask)) << 4;
//std::cout << fmt::format("{:04x}/{:04x} = {:02x}", addr, 0xD000 | (addr & mask), highBits) << std::endl;
auto y = (line - VIDEO_FIRST_VISIBLE_LINE);
auto x = 0;
if(_type == eCDP1861_C10) {
if(y & 1) x = 64;
y >>= 1;
}
for (int j = 0; j < 8; ++j) {
_screen.setPixel(i * 8 + j, (line - VIDEO_FIRST_VISIBLE_LINE), highBits | ((data >> (7 - j)) & 1));
_screen.setPixel(x + i * 8 + j, y, highBits | ((data >> (7 - j)) & 1));
if(_type == eCDP1861_C10 && y == 63) {
_screen.setPixel(x + i * 8 + j, 0, highBits | ((data >> (7 - j)) & 1));
}
}
}
if (_displayEnabledLatch) {
Expand Down
18 changes: 14 additions & 4 deletions src/emulation/videoscreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ class VideoScreen
}
return _palette[_screenBuffer[y * _stride + x]];
}
uint8_t getPixelIndex(int x, int y) const
{
if constexpr (isRGBA()) {
return 0;
}
if(_overlayCellHeight) {
auto overlayPtr = _colorOverlay.data() + (y/_overlayCellHeight)*_overlayCellHeight * 8;
return (_screenBuffer[y * _stride + x] & 0xf) | *overlayPtr << 4;
}
return _screenBuffer[y * _stride + x];
}
PixelType& getPixelRef(int x, int y)
{
return _screenBuffer[y * _stride + x];
Expand Down Expand Up @@ -141,12 +152,11 @@ class VideoScreen
auto dstPtr = destination + row * destinationStride;
auto overlayPtr = _colorOverlay.data() + (row/_overlayCellHeight)*_overlayCellHeight * 8;
for (unsigned x = 0; x < _width; ++x) {
auto pxl = *srcPtr++;
if(_overlayCellHeight < 0) {
*dstPtr++ = pxl ? _palette[7+4] : _palette[_overlayBackground & 3];
if(*srcPtr++) {
*dstPtr++ = _overlayCellHeight < 0 ? _palette[0x71] : _palette[(overlayPtr[x >> 3]<<4) + 1];
}
else {
*dstPtr++ = pxl ? _palette[overlayPtr[x >> 3] + 4] : _palette[_overlayBackground & 3];
*dstPtr++ = _palette[_overlayBackground & 3];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/chip8testhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class HeadlessTestHost : public emu::EmulatorHost
result.reserve(width * height + height);
for (int y = 0; y < height; y += scaleY) {
for (int x = 0; x < width; x += scaleX) {
result.push_back(screen->getPixel(x, y) ? '#' : '.');
result.push_back(screen->getPixelIndex(x, y) & 0xf ? '#' : '.');
}
result.push_back('\n');
}
Expand All @@ -195,7 +195,7 @@ class HeadlessTestHost : public emu::EmulatorHost
if(y >= rect.y && y < rect.y + rect.h) {
for (int x = 0; x < width; x += scaleX) {
if(x >= rect.x && x < rect.x + rect.w) {
result.push_back((screen->getPixel(x, y) & 15) ? '#' : '.');
result.push_back((screen->getPixelIndex(x, y) & 15) ? '#' : '.');
}
}
result.push_back('\n');
Expand Down

0 comments on commit da5bf60

Please sign in to comment.