Skip to content

Commit

Permalink
Merge branch 'sid-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Oct 15, 2023
2 parents 6edacc3 + 80877cd commit 25c7497
Show file tree
Hide file tree
Showing 26 changed files with 1,827 additions and 107 deletions.
92 changes: 50 additions & 42 deletions ComputerSystems/Commodore64/C64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Threading;
using Extensions.Byte;
using System.Windows.Input;
using Hardware.Mos6526Cia;
using System.Threading.Tasks;
using System;
Expand All @@ -11,8 +10,12 @@
using Commodore64.Cia;
using Commodore64.Cartridge;
using Commodore64.Properties;
using System.Windows.Forms;
using Commodore64.Keyboard;
using Commodore64.Sid.NAudioImpl;

namespace Commodore64 {
namespace Commodore64
{
public class C64 {

public const int CLOCK_PAL = 985248;
Expand Down Expand Up @@ -40,11 +43,12 @@ public class C64 {
public Cia1 Cia { get; private set; }
public Cia2 Cia2 { get; private set; }
public VicIi Vic { get; private set; }
public NAudioSid Sid { get; set; }
public C64Bus Memory { get; private set; }
public Cpu Cpu { get; private set; }
public ICartridge Cartridge { get; set; }

public bool KeyboardActivated { get; set; } = false;
public IC64KeyboardInputProvider C64KeyboardInputProvider { get; set; }

public C64() {

Expand All @@ -58,7 +62,10 @@ public void Initialize() {
Vic = new VicIi(TvSystem.PAL) {
C64 = this
};
Memory = new C64Bus(Cia, Cia2, Vic);

Sid = new NAudioSid();

Memory = new C64Bus(Cia, Cia2, Vic, Sid);
Cpu = new Cpu(Memory);

if (Cartridge != null) Memory.InsertCartridge(Cartridge);
Expand All @@ -82,6 +89,7 @@ public void PowerOn() {
if (Settings.Default.KernalWhiteTextColor) PatchKernalRomTextColor(0x01);

Cpu.Reset();
Sid.Play();

_isRunnning = true;
_tcsStop = new TaskCompletionSource<bool>();
Expand Down Expand Up @@ -113,13 +121,14 @@ public void PowerOn() {
});

t.SetApartmentState(ApartmentState.STA);
t.Start();
}

public Task<bool> PowerOff() {
if (_isRunnning == false) return Task.FromResult(true);

Sid.Stop();

_isRunnning = false;
return _tcsStop.Task;
}
Expand All @@ -138,53 +147,52 @@ private void RemoveEventHandlers() {
}

private void CiaReadDataPortB(object sender, EventArgs e) {
if (Cia.DataDirectionA == 0xFF) ScanKeyboard();
}

private void CiaInterrupt(object sender, EventArgs e) {
Cpu.Interrupt();
}

private void Vic_OnGenerateRasterLineInterrupt(object sender, EventArgs e) {
Cpu.Interrupt();
}


public void ScanKeyboard() {
if (!KeyboardActivated) return;

// The BASIC keyboard scanning routine checks if any key is pressed at all.
// This is done by checking all rows at once.
if (Cia.DataPortA == 0) {
Cia.DataPortB = 0x00;
return;
}

// No keys should be pressed when all rows are unset.
if (Cia.DataPortA == 0xFF) {
Cia.DataPortB = 0xFF;
return;
}
// Keyboard Scanning
if (Cia.DataDirectionA == 0xFF)
{
// The BASIC keyboard scanning routine checks if any key is pressed at all.
// This is done by checking all rows at once.
if (Cia.DataPortA == 0)
{
Cia.DataPortB = 0x00;
return;
}

var rowIndexes = ((byte)~Cia.DataPortA).GetSetBitsIndexes();
// No keys should be pressed when all rows are unset.
if (Cia.DataPortA == 0xFF)
{
Cia.DataPortB = 0xFF;
return;
}

foreach (var ri in rowIndexes) {
byte data = 0;
var rowIndexes = ((byte)~Cia.DataPortA).GetSetBitsIndexes();

for (int ci = 0; ci < 8; ci++) {
foreach (var ri in rowIndexes)
{
byte data = 0;

var key = C64Keyboard.Matrix[ri, ci];
if (key == Key.None) continue;
for (int ci = 0; ci < 8; ci++)
{
var key = C64Keyboard.Matrix[ri, ci];
if (key == Keys.None) continue;

if (Keyboard.IsKeyDown(key)) {
data |= (byte)(1 << ci);
if (C64KeyboardInputProvider != null && C64KeyboardInputProvider.IsKeyDown(key))
{
data |= (byte)(1 << ci);
}
}

Cia.DataPortB = (byte)~data;
}

Cia.DataPortB = (byte)~data;
}
}

private void CiaInterrupt(object sender, EventArgs e) {
Cpu.Interrupt();
}

private void Vic_OnGenerateRasterLineInterrupt(object sender, EventArgs e) {
Cpu.Interrupt();
}
}
}
28 changes: 26 additions & 2 deletions ComputerSystems/Commodore64/C64Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Extensions.Byte;
using Extensions.Enums;
using Commodore64.Enums;
using Commodore64.Sid;
using Commodore64.Sid.Enums;

namespace Commodore64 {

Expand Down Expand Up @@ -42,10 +44,10 @@ public class C64Bus : MemoryBase<byte> {
private Cia1 _cia;
private Cia2 _cia2;
private readonly VicIi _vic;

private readonly SidBase _sid;
private ICartridge _cartridge;

public C64Bus(Cia1 cia, Cia2 cia2, VicIi vic) : base(0x10000) {
public C64Bus(Cia1 cia, Cia2 cia2, VicIi vic, SidBase sid) : base(0x10000) {
//_memory.FillWithRandomData();

// Intialize processor addressing mode with default values
Expand All @@ -60,6 +62,7 @@ public C64Bus(Cia1 cia, Cia2 cia2, VicIi vic) : base(0x10000) {
_cia = cia;
_cia2 = cia2;
_vic = vic;
_sid = sid;
}

public void InsertCartridge(ICartridge cartridge) {
Expand Down Expand Up @@ -209,6 +212,17 @@ public override byte Read(int address) {

}

// SID (0xD400 - 0xD7FF, VIC-II register images repeated every $20, 32 bytes)
if (address >= 0xD400 && address <= 0xD7FF)
{

// The SID class has its own indexer which makes it easy to map
// addresses into the VIC-II. The `% 0x20` makes sure that the
// registers available in the VIC-II are mirrored all the way up to
// 0xD7FF.
return _sid[(SidRegister)((address - 0xD400) % 0x20)];
}

// CIA 1
if (address >= 0xDC00 && address <= 0xDCFF) {

Expand Down Expand Up @@ -321,7 +335,17 @@ public override void Write(int address, byte value) {
// registers available in the VIC-II are mirrored all the way up to
// 0xD3FF.
_vic[(Register)((address - 0xD000) % 0x40)] = value;
}

// SID (0xD400 - 0xD7FF, VIC-II register images repeated every $20, 32 bytes)
if (address >= 0xD400 && address <= 0xD7FF)
{

// The SID class has its own indexer which makes it easy to map
// addresses into the VIC-II. The `% 0x20` makes sure that the
// registers available in the VIC-II are mirrored all the way up to
// 0xD7FF.
_sid[(SidRegister)((address - 0xD400) % 0x20)] = value;
}

// CIA 1
Expand Down
2 changes: 1 addition & 1 deletion ComputerSystems/Commodore64/Cia/Cia2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public byte this[Register index] {
return ToBcd(_todHaltedTenths);

case Register.R_0x01_PORT_B:
Debug.WriteLine("Reading PORTB");
//Debug.WriteLine("Reading PORTB");
return _registers[i];

default:
Expand Down
4 changes: 4 additions & 0 deletions ComputerSystems/Commodore64/Commodore64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<Content Include="icon_transparent_32.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NAudio" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Debugger\Debugger.csproj" />
<ProjectReference Include="..\..\Extensions\Extensions.csproj" />
Expand Down
32 changes: 16 additions & 16 deletions ComputerSystems/Commodore64/FormC64Screen.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 25c7497

Please sign in to comment.