Skip to content

Commit

Permalink
better support for unknow commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Berti committed Jan 17, 2019
1 parent 516b562 commit 5dd9810
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
5 changes: 4 additions & 1 deletion simplevgm.iml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
5 changes: 3 additions & 2 deletions src/uk/co/omgdrv/simplevgm/MusicEmu.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ protected void logError()
if (!trackEnded_)
{
trackEnded_ = true;
System.out.println("emulation error");
System.out.println("Unexpected error");
new Exception().printStackTrace();
}
}

Expand Down Expand Up @@ -195,4 +196,4 @@ private void applyFade(byte[] io, int count)
scaleSamples(io, i, n, gain);
}
}
}
}
29 changes: 22 additions & 7 deletions src/Runner.java → src/uk/co/omgdrv/simplevgm/Runner.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uk.co.omgdrv.simplevgm.VGMPlayer;
package uk.co.omgdrv.simplevgm;

import uk.co.omgdrv.simplevgm.model.PsgProvider;
import uk.co.omgdrv.simplevgm.util.Util;

Expand All @@ -19,19 +20,34 @@
public class Runner {

private static boolean DISABLE_PSG = false;
private static String VGM_FOLDER = "/data/emu/vgm/"; //"vgm";
private static String VGM_FILE = "ghostbuster.vgm";
private static String VGM_FOLDER = "."; //"vgm";
private static String VGM_FILE = "file.vgm";

private static Predicate<Path> vgmFilesPredicate = p ->
p.toString().endsWith(".vgm") || p.toString().endsWith(".vgz");


public static void main(String[] args) throws Exception {
Path path = getPathToPlay(args);
boolean isFolder = path.toFile().isDirectory();
System.out.println(String.format("Playing %s: %s",
(isFolder ? "folder" : "file"), path.toAbsolutePath().toString()));
PsgProvider psgProvider = DISABLE_PSG ? PsgProvider.NO_SOUND : null;
VGMPlayer v = VGMPlayer.createInstance(psgProvider, 44100);
Runner r = new Runner();
// r.playAll(v, VGM_FOLDER);
r.playRecursive(v, VGM_FOLDER);
if(isFolder){
r.playRecursive(v, path);
} else {
r.playOne(v, path);
}
}

private static Path getPathToPlay(String[] args){
Path p = Paths.get(".");
if(args.length > 0){
p = Paths.get(args[0]);
}
return p;
}

private void playAll(VGMPlayer v, String folderName) throws Exception {
Expand All @@ -40,8 +56,7 @@ private void playAll(VGMPlayer v, String folderName) throws Exception {
files.stream().forEach(f -> playOne(v, f));
}

private void playRecursive(VGMPlayer v, String folderName) throws Exception {
Path folder = Paths.get(folderName);
private void playRecursive(VGMPlayer v, Path folder) throws Exception {
Set<Path> fileSet = new HashSet<>();
Files.walkFileTree(folder, createFileVisitor(fileSet));
List<Path> list = new ArrayList<>(fileSet);
Expand Down
74 changes: 42 additions & 32 deletions src/uk/co/omgdrv/simplevgm/VgmEmu.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ protected int runMsec(int msec)
break;

default:
switch (cmd & 0xF0)
{
switch (cmd & 0xF0) {
case CMD_PCM_DELAY:
write_pcm(time, data[pcm_pos++] & 0xFF);
time += cmd & 0x0F;
Expand All @@ -254,36 +253,8 @@ protected int runMsec(int msec)
case CMD_SHORT_DELAY:
time += (cmd & 0x0F) + 1;
break;

//unsupported two operands
case 0x30:
case 0x40:
pos += 1;
System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd));
break;

//unsupported two operands
case 0x50:
case 0xA0:
case 0xB0:
System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd));
pos += 2;
break;
//unsupported three operands
case 0xC0:
case 0xD0:
System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd));
pos += 3;
break;
//unsupported four operands
case 0xE0:
case 0xF0:
System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() +", unsupported command: " + Integer.toHexString(cmd));
pos += 4;
break;
default:
System.out.println(String.format("Unexpected command: %s, at position: %s", Integer.toHexString(cmd), Integer.toHexString(pos)));
logError();
handleUnsupportedCommand(cmd);
break;
}
}
Expand All @@ -310,6 +281,45 @@ protected int runMsec(int msec)
return endTime;
}

private void handleUnsupportedCommand(int cmd) {
System.out.println(vgmHeader.getIdent() + vgmHeader.getVersionString() + ", unsupported command: " + Integer.toHexString(cmd));
switch (cmd & 0xF0) {
//unsupported one operand
case 0x30:
case 0x40:
pos += 1;
break;
//unsupported two operands
case 0x50:
case 0xA0:
case 0xB0:
pos += 2;
break;
//unsupported three operands
case 0xC0:
case 0xD0:
pos += 3;
break;
//unsupported four operands
case 0xE0:
case 0xF0:
pos += 4;
break;
case 0x90: //vgm 1.60, dac stream control 0x90 - 0x95
int subCmd = cmd & 0x7;
int diff = subCmd < 2 || subCmd == 5 ? 4 : 5;
diff = subCmd == 3 ? 10 : diff;
diff = subCmd == 4 ? 1 : diff;
pos += diff;
break;
default:
System.out.println(String.format("Unexpected command: %s, at position: %s", Integer.toHexString(cmd), Integer.toHexString(pos)));
logError();
break;
}
}


protected void mixSamples(byte[] out, int out_off, int count)
{
if (fm == null)
Expand All @@ -333,4 +343,4 @@ protected void mixSamples(byte[] out, int out_off, int count)

fm_pos = in_off;
}
}
}

0 comments on commit 5dd9810

Please sign in to comment.