Skip to content

Commit

Permalink
#5 Work towards making the C64 emulator run at correct speed
Browse files Browse the repository at this point in the history
  • Loading branch information
hagronnestad committed Sep 5, 2019
1 parent 03f5795 commit 413efde
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
41 changes: 41 additions & 0 deletions ComputerSystems/Commodore64/C64.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MicroProcessor.Cpu6502;
using System.Diagnostics;
using System.Threading;

namespace Commodore64 {
public class C64 {
Expand All @@ -15,5 +17,44 @@ public C64() {
Cpu.Reset();
}

public void Run() {
// CLOCK_PAL = 985248 Hz
// CLOCK_NTSC = 1022727 Hz

// CLOCK_VICII_PAL = 7881984 Hz
// CLOCK_VICII_NTSC = 8181816 Hz

var cpuClockSpeedPal = 1.0f / 985248;
var swCpuClock = Stopwatch.StartNew();
var swCiaInterrupt = Stopwatch.StartNew();


new Thread(() => {
while (true) {

// CPU clock
if (swCpuClock.Elapsed.TotalMilliseconds > cpuClockSpeedPal) {

// Cycle the CPU
Cpu.Cycle();

// I have to work out how to properly time this
Memory[0xD012] = Memory[0xD012] == 0 ? (byte)1 : (byte)0;

swCpuClock.Restart();
}

// CIA interrupt
if (swCiaInterrupt.Elapsed.TotalMilliseconds > (1000 / 60)) {
Cpu.Interrupt();

swCiaInterrupt.Restart();
}

}

}).Start();
}

}
}
49 changes: 4 additions & 45 deletions ComputerSystems/Commodore64/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Commodore64;
using Commodore64.Enums;
using System;
using System.Threading;
using System.Windows.Forms;

namespace ComputerSystem.Commodore64 {
Expand Down Expand Up @@ -66,7 +65,7 @@ static void Main() {

}

// CTRL modifier
// CTRL modifier
} else if (e.Modifiers == Keys.Control) {
switch (e.KeyCode) {
case Keys.D0:
Expand Down Expand Up @@ -101,7 +100,7 @@ static void Main() {
break;
}

// Shift+CTRL modifier
// Shift+CTRL modifier
} else if (e.Modifiers == (Keys.Shift | Keys.Control)) {
switch (e.KeyCode) {
case Keys.D1:
Expand Down Expand Up @@ -130,7 +129,7 @@ static void Main() {
break;
}

// Shift modifier
// Shift modifier
} else if (e.Modifiers == Keys.Shift) {
switch (e.KeyCode) {
case Keys.Home:
Expand All @@ -148,48 +147,8 @@ static void Main() {
c64.Cpu.Memory[0xC6] = 1;
};

//Stopwatch sw = new Stopwatch();

new Thread(() => {
while (true) {
//sw.Start();

c64.Cpu.Step();

//while (sw.Elapsed.TotalMilliseconds < 0.0004) {

//}

//sw.Stop();
//sw.Reset();

//if (c64.Cpu.TotalInstructions % 100000 == 0) {
// Debug.WriteLine($"{c64.Cpu.TotalInstructions}");
// Debug.WriteLine($"{c64.Cpu.PC:X2}");
//}

// Raster trigger thingy
if ((c64.Cpu.TotalInstructions % 10000) < 30) {
c64.Cpu.Memory[0xD012] = 0;
} else {
c64.Cpu.Memory[0xD012] = 1;
}

// Interrupt from CIA-chip
if (c64.Cpu.TotalInstructions % 30000 == 0) {
c64.Cpu.Interrupt();
}
}

}).Start();

//new Thread(() => {
// while (true) {
// c64.Cpu.Memory[0xD012] = 0;
// Thread.Sleep(20);
// }
//}).Start();

c64.Run();
Application.Run(form);
}
}
Expand Down

0 comments on commit 413efde

Please sign in to comment.