Permalink
Browse files

implement speed control and fix a few things

  • Loading branch information...
1 parent 73a4b10 commit 5644e6761a9ae9b51eb629d969429a2ffe010574 @BonsaiDen committed May 22, 2011
Showing with 81 additions and 81 deletions.
  1. +7 −3 cpu/8080.c
  2. +2 −2 cpu/8080.h
  3. BIN main
  4. +64 −68 main.c
  5. +8 −8 ops/ctrl.c
View
@@ -43,7 +43,9 @@ void cpu_fetch() {
}
-void cpu_exec() {
+unsigned int cpu_exec() {
+
+ unsigned long cycle_count = CPU->cycle_count;
// call op code function
uint8_t inst = CPU->instruction;
@@ -55,8 +57,10 @@ void cpu_exec() {
*CPU->PC &= 0xffff; // mask PC
// add min cycle count, extra cycles are added inside the op code functions
- CPU->cycles += OP_CODE_DATA[inst * 3 + 1];
+ CPU->cycle_count += OP_CODE_DATA[inst * 3 + 1];
+ return CPU->cycle_count - cycle_count;
+
}
void cpu_reset() {
@@ -72,7 +76,7 @@ void cpu_reset() {
// state info
CPU->instruction = 0;
- CPU->cycles = 0;
+ CPU->cycle_count = 0;
CPU->ime = 0;
// Pins
View
@@ -29,7 +29,7 @@ typedef struct CPU_8080 {
// state info
uint8_t instruction;
- unsigned long cycles;
+ unsigned long cycle_count;
uint8_t ime;
// Pins
@@ -42,7 +42,7 @@ CPU_8080 *CPU;
void cpu_init();
void cpu_fetch();
-void cpu_exec();
+unsigned int cpu_exec();
void cpu_reset();
void cpu_destroy();
View
BIN main
Binary file not shown.
View
132 main.c
@@ -12,29 +12,17 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
-#include <curses.h>
#include <unistd.h>
+#include <sys/timeb.h>
+
uint8_t *MEMORY;
-WINDOW *cpu_win;
-WINDOW *mem_win;
-static void run();
+static void emulate();
int main(int argc, const char *argv[]) {
- // Setup
- initscr();
- cbreak();
- noecho();
- keypad(stdscr, TRUE);
- nodelay(stdscr, TRUE);
-
- cpu_win = newwin(LINES, COLS / 2, 15, 0);
- mem_win = newwin(LINES, COLS / 2 - 1, 0, COLS / 2 + 1);
- scrollok(mem_win, TRUE);
-
// Init memory
MEMORY = calloc(65536, sizeof(uint8_t));
uint8_t TEST[65536] = {
@@ -66,71 +54,79 @@ int main(int argc, const char *argv[]) {
// Start CPU
cpu_init();
*CPU->SP = 65535;
- run();
+ emulate();
- // cleanup
- free(mem_win);
- free(cpu_win);
free(MEMORY);
-
cpu_destroy();
- endwin();
return 0;
}
-static void run() {
-
- int ch = 0;
- int paused = 0, first = 1, count = 0;
- while(1) {
-
- if (!paused) {
-
- uint8_t inst = 0;
- if (!first) {
-
- cpu_fetch();
- inst = CPU->instruction;
-// wprintw(mem_win, "%-5s(%02x)\n", OP_CODE_NAMES[inst], inst);
- cpu_exec();
-
- }
-
- if (count > 2000) {
-
- uint8_t flags = *CPU->F;
- wmove(cpu_win, 0, 0);
- wprintw(cpu_win, "\n (F) %c %c %c %c %c %c %c %c\n",
- (flags & 128) ? 'S' : '-',
- (flags & 64) ? 'Z' : '-',
- (flags & 32) ? '0' : ' ',
- (flags & 16) ? 'A' : '-',
- (flags & 8) ? '0' : ' ',
- (flags & 4) ? 'P' : '-',
- (flags & 2) ? '1' : ' ',
- (flags & 1) ? 'C' : '-' );
-
- wprintw(cpu_win, " (A) %03d (PSW) %05d (OP) %-5s\n", *CPU->A, *CPU->PSW, OP_CODE_NAMES[inst]);
- wprintw(cpu_win, " (B) %03d (C) %03d (BC) %05d\n", *CPU->B, *CPU->C, *CPU->BC);
- wprintw(cpu_win, " (D) %03d (E) %03d (DE) %05d\n", *CPU->D, *CPU->E, *CPU->DE);
- wprintw(cpu_win, " (H) %03d (L) %03d (HL) %05d\n", *CPU->H, *CPU->L, *CPU->HL);
- wprintw(cpu_win, " (SP) %05d (PC) %05d\n", *CPU->SP, *CPU->PC);
- wprintw(cpu_win, "(CYC) %17lu\n", CPU->cycles);
- wrefresh(cpu_win);
- count = 0;
- }
+static unsigned long now() {
+ struct timeb tp;
+ ftime(&tp);
+ return tp.millitm + tp.time * 1000;
+}
+
+
+static void emulate() {
+
+ unsigned int clock_speed = 2,
+ cycles_per_second = 0,
+ second_time = 0,
+ time_taken = 10;
+
+ unsigned long last_time = now();
+
+ for(;;) {
+
+ unsigned long start_time = now();
+ unsigned long cycle_end = CPU->cycle_count + clock_speed * 10000;
+ while(CPU->cycle_count < cycle_end) {
+ cpu_fetch();
+ cycles_per_second += cpu_exec();
}
- count++;
-// wrefresh(mem_win);
+ // Speed control
+ usleep(10000 - time_taken * 20); // sleep ~10 ms - some other stuff
+
+ time_taken = now() - start_time;
+ second_time += time_taken;
+
+ // Log Mhz every second
+ if (second_time >= 1000) {
+
+ double cur_clock_speed = (double)cycles_per_second / 1000000;
+ printf("Clocking at %.2f Mhz\n", cur_clock_speed);
+
+ cycles_per_second = 0;
+ second_time -= 1000;
+
+ }
-// usleep(1);
- first = 0;
-
}
-
+
}
+// if (counter <= 0) {
+// printf("Hello World!\n");
+// counter += vblank_interrupt;
+// }
+
+
+//
+// unsigned int CPU_HZ = 2000000;
+// unsigned int SCREEN_HZ = 50;
+// unsigned int VBLANK = CPU_HZ / SCREEN_HZ;
+// unsigned int SCANLINES = 256;
+// unsigned int SCREEN_LINES = 212;
+// unsigned int vblank_interrupt = (SCANLINES - SCREEN_LINES) * VBLANK / SCANLINES;
+// unsigned int CYCLE_DURATION = 1000000 / CPU_HZ;
+//
+// printf("%d\n", vblank_interrupt);
+//
+// int counter = vblank_interrupt;
+//
+//
View
@@ -43,42 +43,42 @@ static inline void ret() {
}\
static void PREFIX##NZ() {\
if (!(*CPU->F & 64)) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##Z() {\
if (*CPU->F & 64) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##NC() {\
if (!(*CPU->F & 1)) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##C() {\
if (*CPU->F & 1) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##PO() {\
if (!(*CPU->F & 4)) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##PE() {\
if (*CPU->F & 4) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##P() {\
if (!(*CPU->F & 128)) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\
static void PREFIX##M() {\
if (*CPU->F & 128) {\
- METHOD(); CPU->cycles += CYCLES;\
+ METHOD(); CPU->cycle_count += CYCLES;\
}\
}\

0 comments on commit 5644e67

Please sign in to comment.