Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ The available environment variables are:
properly, some other will ignore this and setup the text
mode again.

- `EMU_SLOW_FACTOR` Set a slowing factor, this forces the CPU to take at
least for the given amount of nanoseconds for every
opcode being executed.


Simple Example
--------------
Expand Down
25 changes: 25 additions & 0 deletions src/cpu.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "cpu.h"
#include "dbg.h"
#include "dis.h"
#include "emu.h"
#include "os.h"
#include "env.h"

// Forward declarations
static void do_instruction(uint8_t code);
Expand Down Expand Up @@ -2550,12 +2553,34 @@ static void do_instruction(uint8_t code)
};
}

static long slow_ns = 0;

static void slow_cpu(struct timespec *start)
{
if(!slow_ns)
return;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
long left = slow_ns -
(now.tv_sec - start->tv_sec) * 1000000000L -
(now.tv_nsec - start->tv_nsec);
if (left > 0)
clock_nanosleep(CLOCK_MONOTONIC, 0,
&(struct timespec){left / 1000000000L,
left % 1000000000L}, NULL);
}

void execute(void)
{
if (getenv(ENV_SLOW))
slow_ns = atol(getenv(ENV_SLOW));
for(; !exit_cpu;)
{
struct timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
handle_irq();
next_instruction();
slow_cpu(&start);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
#define ENV_LOWMEM "EMU2_LOWMEM"
#define ENV_ROWS "EMU2_ROWS"
#define ENV_DOSVER "EMU2_DOSVER"
#define ENV_SLOW "EMU_SLOW_FACTOR"