Skip to content

Commit

Permalink
[#314] zx-spectrum z80: 3.5MHz
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Jan 27, 2023
1 parent 98882cf commit 3d3fb5c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 21 deletions.
91 changes: 91 additions & 0 deletions application/src/main/files/config/ZxSpectrum48K.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name = "ZX Spectrum 48K"

[MEMORY]
schemaPoint = "220,60"
path = "byte-mem.jar"
name = "byte-mem"
id = "1e13a25d-fb23-4061-853a-5fe996b03281"
type = "MEMORY"

[MEMORY.settings]
#memorySize = 49152
banksCount = 0
ROMfrom0 = 0
imageName0 = "/home/vbmacher/tmp/emuStudio/z80full.out"
imageName1 = "/home/vbmacher/tmp/emuStudio/examples/zxspectrum-48k/48.rom"
imageAddress1 = 0
imageBank1 = 0
imageBank0 = 0
commonBoundary = 0
imageAddress0 = 32768
ROMto0 = 16383

# memorySize = 65536
# ROM areas are stored as pairs ROMfromN and ROMtoN, where N is 0-based counter. There can be multiple rom areas.
# ROMfromN = someAddress
# ROMtoN = someAddress
# Memory "images" (files) will be loaded at startup into memory. They are stored here as triplet:
# - imageNameN (file name),
# - imageAddressN (address where the file will be loaded),
# - imageBankN (if memory has >1 banksCount, then number of bank; 0 otherwise)
#
# where N is a 0-based counter
# imageNameN = imageFileName # if the suffix ends with ".hex", the format will be Intel HEX; otherwise it will be binary
# imageAddressN = ...
# imageBankN = 0
[COMPILER]
schemaPoint = "80,60"
path = "as-z80.jar"
settings = {}
name = "as-z80"
id = "9233e0c2-b53c-41e7-9eca-bbb264fcd9da"
type = "COMPILER"

[CPU]
schemaPoint = "220,180"
path = "z80-cpu.jar"
settings = {}
name = "z80-cpu"
id = "b86d4bc2-632c-46e3-bba1-c088c9177983"
type = "CPU"

[CPU.settings]
frequency_khz = 3500

# Uncomment the following for specific settings (and remove settings = {} above)
# [CPU.settings]
# printCode = true
# printCodeUseCache = true
# printCodeFileName = "syserr" # Or custom path to a file
[[DEVICE]]
schemaPoint = "380,180"
path = "zxspectrum-display.jar"
settings = {}
name = "zxspectrum-display"
id = "1436ac2d-982f-4a52-b7c6-ecb6f2a0440d"
type = "DEVICE"

[[connections]]
bidirectional = true
from = "9233e0c2-b53c-41e7-9eca-bbb264fcd9da"
to = "1e13a25d-fb23-4061-853a-5fe996b03281"
points = []

[[connections]]
bidirectional = true
from = "1e13a25d-fb23-4061-853a-5fe996b03281"
to = "b86d4bc2-632c-46e3-bba1-c088c9177983"
points = []

[[connections]]
bidirectional = true
from = "b86d4bc2-632c-46e3-bba1-c088c9177983"
to = "1436ac2d-982f-4a52-b7c6-ecb6f2a0440d"
points = []

[[connections]]
bidirectional = true
from = "1e13a25d-fb23-4061-853a-5fe996b03281"
to = "1436ac2d-982f-4a52-b7c6-ecb6f2a0440d"
points = ["360,60"]

Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
public class CpuImpl extends AbstractCPU {
private static final Logger LOGGER = LoggerFactory.getLogger(CpuImpl.class);

private static final String PRINT_CODE = "printCode";
private static final String PRINT_CODE_USE_CACHE = "printCodeUseCache";

private final ScheduledExecutorService frequencyScheduler = Executors.newSingleThreadScheduledExecutor();
private final AtomicReference<Future> frequencyUpdaterFuture = new AtomicReference<>();

Expand All @@ -75,6 +72,8 @@ public CpuImpl(long pluginID, ApplicationApi applicationApi, PluginSettings sett
);
}

context.setCPUFrequency(settings.getInt("frequency_khz", ContextZ80Impl.DEFAULT_FREQUENCY_KHZ));

initializer = new InitializerZ80(
this, pluginID, applicationApi.getContextPool(), settings, context
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public EmulatorEngine(MemoryContext<Byte> memory, ContextZ80Impl context) {
LOGGER.info("Sleep precision: " + SleepUtils.SLEEP_PRECISION + " nanoseconds.");
}

@SuppressWarnings("unused")
public static String intToFlags(int flags) {
String flagsString = "";
if ((flags & FLAG_S) == FLAG_S) {
Expand Down Expand Up @@ -1241,10 +1242,12 @@ int I_LDIR() {
}

int I_INI() {
byte value = context.readIO((regs[REG_B] << 8) | regs[REG_C]);
int bc = (regs[REG_B] << 8) | regs[REG_C];
byte value = context.readIO(bc);
memptr = (bc + 1) & 0xFFFF;

int address = (regs[REG_H] << 8) | regs[REG_L];
memory.write(address, value);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) + 1) & 0xFFFF;

regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
address++;
Expand All @@ -1258,10 +1261,11 @@ int I_INI() {
}

int I_INIR() {
byte value = context.readIO((regs[REG_B] << 8) | regs[REG_C]);
int bc = (regs[REG_B] << 8) | regs[REG_C];
byte value = context.readIO(bc);
int address = (regs[REG_H] << 8) | regs[REG_L];
memory.write(address, value);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) + 1) & 0xFFFF;
memptr = (bc + 1) & 0xFFFF;

regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
address++;
Expand Down Expand Up @@ -1293,10 +1297,11 @@ int I_INIR() {
}

int I_IND() {
byte value = context.readIO((regs[REG_B] << 8) | regs[REG_C]);
int bc = (regs[REG_B] << 8) | regs[REG_C];
byte value = context.readIO(bc);
int hl = (regs[REG_H] << 8) | regs[REG_L];
memory.write(hl, value);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) - 1) & 0xFFFF;
memptr = (bc - 1) & 0xFFFF;

regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
hl = (hl - 1) & 0xFFFF;
Expand All @@ -1309,10 +1314,11 @@ int I_IND() {
}

int I_INDR() {
byte value = context.readIO((regs[REG_B] << 8) | regs[REG_C]);
int bc = (regs[REG_B] << 8) | regs[REG_C];
byte value = context.readIO(bc);
int hl = (regs[REG_H] << 8) | regs[REG_L];
memory.write(hl, value);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) - 1) & 0xFFFF;
memptr = (bc - 1) & 0xFFFF;

regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
hl = (hl - 1) & 0xFFFF;
Expand Down Expand Up @@ -1345,13 +1351,14 @@ int I_INDR() {
int I_OUTI() {
int address = (regs[REG_H] << 8) | regs[REG_L];
byte value = memory.read(address);
context.writeIO((regs[REG_B] << 8) | regs[REG_C], value);
int bc = (regs[REG_B] << 8) | regs[REG_C];
context.writeIO(bc, value);

address++;
regs[REG_H] = (address >>> 8) & 0xFF;
regs[REG_L] = address & 0xFF;
regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) + 1) & 0xFFFF;
memptr = (bc + 1) & 0xFFFF;

flags = FLAG_N | (regs[REG_B] == 0 ? FLAG_Z : 0) | (flags & FLAG_C) | (flags & FLAG_S) | (flags & FLAG_PV) | (flags & FLAG_H);
return 16;
Expand All @@ -1360,13 +1367,14 @@ int I_OUTI() {
int I_OTIR() {
int address = (regs[REG_H] << 8) | regs[REG_L];
byte value = memory.read(address);
context.writeIO((regs[REG_B] << 8) | regs[REG_C], value);
int bc = (regs[REG_B] << 8) | regs[REG_C];
context.writeIO(bc, value);

address++;
regs[REG_H] = (address >>> 8) & 0xFF;
regs[REG_L] = address & 0xFF;
regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) + 1) & 0xFFFF;
memptr = (bc + 1) & 0xFFFF;

flags |= FLAG_Z | FLAG_N; // FLAG_Z is set b/c it is expected that OTIR will be repeated until B=0

Expand All @@ -1393,13 +1401,14 @@ int I_OTIR() {
int I_OUTD() {
int address = (regs[REG_H] << 8) | regs[REG_L];
byte value = memory.read(address);
context.writeIO((regs[REG_B] << 8) | regs[REG_C], value);
int bc = (regs[REG_B] << 8) | regs[REG_C];
context.writeIO(bc, value);

address--;
regs[REG_H] = (address >>> 8) & 0xFF;
regs[REG_L] = address & 0xFF;
regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) - 1) & 0xFFFF;
memptr = (bc - 1) & 0xFFFF;

flags = FLAG_N | (regs[REG_B] == 0 ? FLAG_Z : 0) | (flags & FLAG_C) | (flags & FLAG_S) | (flags & FLAG_PV) | (flags & FLAG_H);
return 16;
Expand All @@ -1408,13 +1417,14 @@ int I_OUTD() {
int I_OTDR() {
int address = (regs[REG_H] << 8) | regs[REG_L];
byte value = memory.read(address);
context.writeIO((regs[REG_B] << 8) | regs[REG_C], value);
int bc = (regs[REG_B] << 8) | regs[REG_C];
context.writeIO(bc, value);

address--;
regs[REG_H] = (address >>> 8) & 0xFF;
regs[REG_L] = address & 0xFF;
regs[REG_B] = ((regs[REG_B] - 1) & 0xFF);
memptr = (((regs[REG_B] << 8) | regs[REG_C]) - 1) & 0xFFFF;
memptr = (bc - 1) & 0xFFFF;

flags |= FLAG_Z | FLAG_N;

Expand Down Expand Up @@ -1491,8 +1501,9 @@ int I_IN_A_REF_N() {
int port = memory.read(PC) & 0xFF;
PC = (PC + 1) & 0xFFFF;
//_memPtr = (A << 8) + _memory.ReadByte(PC) + _memory.ReadByte(PC + 1) * 256 + 1;
memptr = ((regs[REG_A] << 8) + port + 1) & 0xFFFF;
regs[REG_A] = (context.readIO((regs[REG_A] << 8) | port) & 0xFF);
int aport = (regs[REG_A] << 8) | port;
regs[REG_A] = context.readIO(aport) & 0xFF;
memptr = (aport + 1) & 0xFFFF;
return 11;
}

Expand Down

0 comments on commit 3d3fb5c

Please sign in to comment.