Skip to content

Commit

Permalink
[#314] Implement tape playback
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed May 12, 2023
1 parent c5424ed commit fa0a580
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum CassetteState {
CLOSED // terminal state
}

private final Loader.PlaybackListener listener;
private final Loader.TapePlayback listener;
private final ExecutorService playPool = Executors.newFixedThreadPool(1);

private final Object stateLock = new Object();
Expand All @@ -55,7 +55,7 @@ public enum CassetteState {

private final Queue<CassetteState> stateNotifications = new ConcurrentLinkedQueue<>();

public CassetteController(Loader.PlaybackListener listener) {
public CassetteController(Loader.TapePlayback listener) {
this.listener = Objects.requireNonNull(listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@
import net.emustudio.emulib.plugins.PluginInitializationException;
import net.emustudio.emulib.plugins.annotations.PLUGIN_TYPE;
import net.emustudio.emulib.plugins.annotations.PluginRoot;
import net.emustudio.emulib.plugins.cpu.CPUContext;
import net.emustudio.emulib.plugins.cpu.TimedEventsProcessor;
import net.emustudio.emulib.plugins.device.AbstractDevice;
import net.emustudio.emulib.plugins.device.DeviceContext;
import net.emustudio.emulib.runtime.ApplicationApi;
import net.emustudio.emulib.runtime.ContextPool;
import net.emustudio.emulib.runtime.settings.PluginSettings;
import net.emustudio.plugins.device.cassette_player.gui.CassettePlayerGui;
import net.emustudio.plugins.device.zxspectrum.bus.api.ZxSpectrumBus;

import javax.swing.*;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.function.Supplier;

@PluginRoot(type = PLUGIN_TYPE.DEVICE, title = "Cassette Player")
public class DeviceImpl extends AbstractDevice {
Expand All @@ -40,7 +45,7 @@ public class DeviceImpl extends AbstractDevice {

private CassettePlayerGui gui;
private CassetteController controller;
private PlaybackListenerImpl cassetteListener;
private TapePlaybackImpl cassetteListener;

public DeviceImpl(long pluginID, ApplicationApi applicationApi, PluginSettings settings) {
super(pluginID, applicationApi, settings);
Expand All @@ -51,12 +56,25 @@ public DeviceImpl(long pluginID, ApplicationApi applicationApi, PluginSettings s
@SuppressWarnings("unchecked")
@Override
public void initialize() throws PluginInitializationException {
ContextPool contextPool = applicationApi.getContextPool();

// a cassette player needs a device to which it will write at its own pace
DeviceContext<Byte> lineIn = applicationApi.getContextPool().getDeviceContext(pluginID, DeviceContext.class);
if (lineIn.getDataType() != Byte.class) {
throw new PluginInitializationException("Could not find line-in device");
Supplier<TimedEventsProcessor> tep; // Line-in device initialization might be happening just now
DeviceContext<Byte> lineIn;
try {
CPUContext cpu = contextPool.getCPUContext(pluginID);
lineIn = contextPool.getDeviceContext(pluginID, DeviceContext.class);
if (lineIn.getDataType() != Byte.class) {
throw new PluginInitializationException("Could not find line-in device");
}
tep = cpu.getTimedEventsProcessor()::get;
} catch (PluginInitializationException ignored) {
ZxSpectrumBus bus = contextPool.getDeviceContext(pluginID, ZxSpectrumBus.class);
lineIn = bus;
tep = () -> bus.getTimedEventsProcessor().orElseThrow();
}
this.cassetteListener = new PlaybackListenerImpl(lineIn);

this.cassetteListener = new TapePlaybackImpl(lineIn, tep);
this.controller = new CassetteController(cassetteListener);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* This file is part of emuStudio.
*
* Copyright (C) 2006-2023 Peter Jakubčo
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.emustudio.plugins.device.cassette_player;

import net.emustudio.emulib.plugins.cpu.TimedEventsProcessor;
import net.emustudio.emulib.plugins.device.DeviceContext;
import net.emustudio.plugins.device.cassette_player.gui.CassettePlayerGui;
import net.emustudio.plugins.device.cassette_player.loaders.Loader;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

/**
* Tape playback.
* <p>
* ZX-Spectrum 48K pulses (for TAP file):
* For each block:
* - on header block: 8063 pulses of 2168 t-states
* - on data block: 3223 pulses of 2168 t-states
* - sync1 (667 t-states)
* - sync2 (735 t-states)
* - block data: 2 pulses of 855 t-states (zero) or 1710 t-states (one) for each byte
* (this is: flag (1 byte), data (xx - flag - checksum), checksum (1 byte)
* <p>
* Links:
* - <a href="https://worldofspectrum.org/faq/reference/48kreference.htm">ZX-Spectrum 48K Technical Reference</a>
* - <a href="https://softspectrum48.weebly.com/notes/tape-loading-routines">Tape loading routines</a>
* - <a href="https://sinclair.wiki.zxnet.co.uk/wiki/Spectrum_tape_interface">Spectrum tape interface</a>
*/
public class TapePlaybackImpl implements Loader.TapePlayback {
private final static int LEADER_PULSE_TSTATES = 2168;
private final static int SYNC1_PULSE_TSTATES = 667;
private final static int SYNC2_PULSE_TSTATES = 735;
private final static int SYNC3_PULSE_TSTATES = 954;
private final static int PAUSE_PULSE_TSTATES = 7000000;
private final static int HEADER_LEADER_PULSE_COUNT = 8063;
private final static int DATA_LEADER_PULSE_COUNT = 3223;
private final static int DATA_PULSE_ONE_TSTATES = 1710;
private final static int DATA_PULSE_ZERO_TSTATES = 855;

private final DeviceContext<Byte> lineIn;
private final AtomicReference<CassettePlayerGui> gui = new AtomicReference<>();
private final Supplier<TimedEventsProcessor> tepSupplier;
private TimedEventsProcessor tep = null;

private final Map<Integer, Runnable> loaderSchedule = new HashMap<>();
private int currentTstates;
private boolean pulseUp;

public TapePlaybackImpl(DeviceContext<Byte> lineIn, Supplier<TimedEventsProcessor> tepSupplier) {
this.lineIn = Objects.requireNonNull(lineIn);
this.tepSupplier = Objects.requireNonNull(tepSupplier);
}

public void setGui(CassettePlayerGui gui) {
this.gui.set(gui);
}

@Override
public void onFileStart() {
loaderSchedule.clear();
currentTstates = 1;
pulseUp = true;
}

@Override
public void onHeaderStart() {
for (int i = 0; i < HEADER_LEADER_PULSE_COUNT; i++) {
schedulePulse(LEADER_PULSE_TSTATES);
}
schedulePulse(SYNC1_PULSE_TSTATES);
schedulePulse(SYNC2_PULSE_TSTATES);
}

@Override
public void onDataStart() {
for (int i = 0; i < DATA_LEADER_PULSE_COUNT; i++) {
schedulePulse(LEADER_PULSE_TSTATES);
}
schedulePulse(SYNC1_PULSE_TSTATES);
schedulePulse(SYNC2_PULSE_TSTATES);
}

@Override
public void onBlockFlag(int flag) {
transmitByte(flag);
}

@Override
public void onProgram(String filename, int dataLength, int autoStart, int programLength) {
log(filename + " : PROGRAM (start=" + autoStart + ", length=" + programLength + ")");
}

@Override
public void onNumberArray(String filename, int dataLength, char variable) {
log(filename + " : NUMBER ARRAY (variable=" + variable + ")");
}

@Override
public void onStringArray(String filename, int dataLength, char variable) {
log(filename + " : STRING ARRAY (variable=" + variable + ")");
}

@Override
public void onMemoryBlock(String filename, int dataLength, int startAddress) {
log(filename + " : MEMORY BLOCK (start=" + startAddress + ")");
}

@Override
public void onBlockData(byte[] data) {
for (byte d : data) {
transmitByte(d & 0xFF);
}
}

@Override
public void onBlockChecksum(byte checksum) {
transmitByte(checksum & 0xFF);
// schedulePulse(SYNC3_PULSE_TSTATES);
// pulseUp = false;
// schedulePulse(PAUSE_PULSE_TSTATES);
}

@Override
public void onFileEnd() {
playPulses();
}

@Override
public void onStateChange(CassetteController.CassetteState state) {
Optional.ofNullable(gui.get()).ifPresent(g -> g.setCassetteState(state));
}

private void log(String message) {
Optional.ofNullable(gui.get()).ifPresent(g -> g.setMetadata(message));
}

private void transmitByte(int data) {
int mask = 0x80; // 1000 0000
while (mask != 0) {
int pulseLength = ((data & mask) == 0) ? DATA_PULSE_ZERO_TSTATES : DATA_PULSE_ONE_TSTATES;
schedulePulse(pulseLength); // 2x according to https://sinclair.wiki.zxnet.co.uk/wiki/Spectrum_tape_interface
schedulePulse(pulseLength);
mask >>>= 1;
}
}

private void schedulePulse(int length) {
Runnable one = () -> lineIn.writeData((byte) 1);
Runnable zero = () -> lineIn.writeData((byte) 0);

loaderSchedule.put(currentTstates, pulseUp ? one : zero);
currentTstates += length;
pulseUp = !pulseUp;
}

private void playPulses() {
if (tep == null) {
tep = tepSupplier.get();
}
tep.scheduleOnceMultiple(loaderSchedule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ private void initComponents() {
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnBrowse)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnLoad)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUnload))
.addComponent(btnLoad))
.addGroup(panelTapeSelectionLayout.createSequentialGroup()
.addComponent(lblTapes)
.addGap(0, 0, Short.MAX_VALUE))
Expand All @@ -169,7 +167,6 @@ private void initComponents() {
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panelTapeSelectionLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(btnBrowse)
.addComponent(btnUnload)
.addComponent(btnLoad)
.addComponent(btnRefresh))
.addContainerGap())
Expand Down Expand Up @@ -211,6 +208,7 @@ private void initComponents() {
.addComponent(lblStatus, GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
.addComponent(panelMetadata, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(panelTapeControlLayout.createSequentialGroup()
.addComponent(btnUnload)
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btnPlay)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
Expand All @@ -225,6 +223,7 @@ private void initComponents() {
.addComponent(panelMetadata, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(panelTapeControlLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(btnUnload)
.addComponent(btnPlay)
.addComponent(btnStop))
.addContainerGap())
Expand Down
Loading

0 comments on commit fa0a580

Please sign in to comment.