Study Box (iNES 186) Audio Tape Recording

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Study Box (iNES 186) Audio Tape Recording

Post by Sour »

Based on Санчез's page linked on mapper 186's wiki page, it seems like he did not have access to any of the audio data and was unable to get any further emulating this device.

A Mesen user has just sent me the audio recordings for 2 different audio tapes meant to be used with the Study Box game.
The audio data on the tapes is stereo, with one channel being actual audio (probably played via the tape reader's speaker?) and the other channel is data.
I can only tell that some portions of the audio sound similar to good old modems (i.e the data), and the rest is probably padding between data sections (a large portion of the audio is just a continuous tone, and sometimes silence).

I quickly tried using "minimodem" to attempt to extract the data, but it looks like the program cannot automatically detect the correct settings to use to decode the audio, and instead relies on command line parameters. Which means I get different results depending on what settings I try, and I have no real way to determine if the result is correct, or completely wrong.

I imagine someone who actually has a clue about these sort of things (i.e not me!) would be able to figure out how the audio is encoded, and how to get it back into binary format. Anyone feel like giving this a shot? I've attached a screenshot of what the audio looks like (~1/10th of a second worth of data) when it sounds like actual data.
Attachments
AudioData.png
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by lidnariq »

Well, it looks more like CUTS (equal bittimes for 1 and 0, 50% duty regardless) than, say, No, I'm wrong, it looks more like SharpMZ, where bits are long or short instead of constantly spaced.

But really the useful place to look is at the starts (lead-in and first data bits) and end.

Also... shouldn't it be practical to find the decoding loop inside the game to figure out what encoding it's looking for?
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

It did seem to me that the main difference in the signal was the width of each spike, too.

It looks like I was making some incorrect assumptions.
I thought the tape player would be sending decoded binary data to the NES, but what you're saying is that typically the audio data would be fed directly to the NES, and the game's code would read the audio & decode it into binary data?

In that case I'm assuming the concept might be similar to the "Family BASIC Data Recorder"? i.e a 1-bit ADC that the program can read from via a register? (mind you, a 1-bit ADC is akin to witchcraft to me!)
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by lidnariq »

From the recording, it seems like it's ... at least mostly CPU-driven.

As far as I know, no audio cassette storage ever did anything particularly more clever...

But ... man, this is not clearly anything:
AudioData-more-subset-annoated.png
AudioData-more-subset-annoated.png (2.13 KiB) Viewed 12997 times
The gaps between extrema really should appear to be bimodal, but I don't think I see that.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

I spent some time trying to figure out how the software itself works, hoping to figure out where to feed the audio data.
Did not have much luck though.

Here's what I found out so far (that's not written on the wiki) - sorry for the wall of text!

$4202 (read)
-Bit 3 indicates a power error and displays a screen to tell the user how to connect the device properly
-Bit 6 seems to be some sort of "ready" or "playing" flag. After the title screen, this needs to toggle between 0 and 1 a number of times (~7-8) for the following screen to start properly (otherwise game freezes).

$4202 (write)
-Bit 4 appears to control the value of bit 6 when reading ("ready flag"). There seems to be a delay between setting bit 4, and reading the corresponding value in bit 6. Implementing this behavior makes the page selection control on the startup screen work without hardcoding $40 as the return value for $4202 (like the wiki says). It also makes the "loading" animation & sound work correctly on the screen following the page selection screen. (in FCEUX/Nestopia, this screen freezes and emits a high pitch sound)

$4201 (read)
-Bit 4: Checked on the title screen - needs to be set for the bit 5 behavior to work
-Bit 5: If bit 6 is also set, toggling bit 5 increments the left-side "page number" on the "loading" screen.
-Bit 6: So long as bit 6 is set, the bit 5 behavior will continue working. If bit 6 is cleared, the ROM completely stops reading from both $4202 & $4201 and you just get the loading screen animation forever. I have no clue what it expects from there, or what this bit means.

These 5 bits ($4201.4/5/6 + $4202.3/6) are the only bits that are ever checked by the ROM for the execution paths I've managed to discover so far.

From videos I've seen, the loading screen seems to start with the left-side number at 3-4, and then counts DOWN to 1 (which is the page that was selected by the people recording the videos). I haven't figured out how to make it count down instead of up - I know it is possible, I got the behavior once while forcing the execution path to take branches that hadn't been taken yet, but I was not paying attention at the time and could not find it again.

The game also reads/writes to a number of memory addresses in the $4400-$4FFF range. Not setting this as RAM breaks the title screen, so this is probably meant to be ram, like the wiki says. It also seems to read bits 0 and 1 of both $4016 & $4017, but at the very least, during the loading screen, both of these do not appear to have any impact.

At this point though, I don't know what else to try to make the ROM try to transfer data or get beyond the loading screen.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by lidnariq »

Here's my hunch as to what some of the questionable bits mean:

R$4202 & $40 — "cassette motor active"

W$4202 & $10 — "request play"
Since there's no external buttons, it seems likely that one of the other bits is "rewind".

R$4201 — Maybe there's some kind of quadrature encoding here??

Also, this video...

Could you provide CPU addresses / ROM offsets for the loops you have found? I'm have an awful time stumbling through things using FCEUX's debugger.



(P.S. searching for 福武書店 スタディボックス finds a lot of japanese-language hits ... but only the auctions look potentially relevant for emulation)
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

Thanks for taking a look! Hopefully you can find something I missed. 6502 assembly (or just assembly in general!) is not something I am used to reading, I can somewhat follow it, but end up misunderstanding the code pretty often.

I agree about R$4202 & $40 / W$4202 & $10, that is a pretty likely explanation for them.
R$4201 is a bit of a mystery though. Really wish I could figure out how to make the page count go down, like it's doing in the 2 videos I've found.

I did some quick searches in Japanese as well, but couldn't really find too much info. I'm relatively fluent in Japanese, so if you do happen to find anything that looks like it might be helpful, let me know!
I did notice that naruko also tried looking into this a while ago (http://seesaawiki.jp/famicomcartridge/ - page hasn't been updated in almost 2 years though.), there's a note saying: "I really want to investigate the Study Box, if someone is willing to help with the analysis please contact me" (I'm assuming they were looking for someone that actually owns the device)


Addresses (CPU memory):

Title Screen:
$E9C4: Checks $4202 & $40 - Title screen is disabled until bit 6 is set
$FA46: Checks $4201 & $10

Loading screen init:
Function starts at $FDB3 and is called 7-8 times at the start of the loading sequence
$FDC6: Loops until $4202 & $40 is set. Then writes twice to $4202 (first with $10 off, waits in $FDE9, and then writes again with $10 on)
$FDDC: Loops until $4202 & $40 is cleared

Loading screen, after init (sound/animation playing):
Function starts at $FAAB and is called in a loop,
$FABD: Checks if $4201 & $40 is set, if not, this function no longer gets called (did not investigate why)
$FAFB: Checks $4201 & $20 - toggling this ends up increasing the page number on the left side

Beyond that, these expansion ram addresses are used (read and/or write):
$4412 = delay counter for on-screen animation, not important
$4418, $4434, $4500, $4501, $4502, $4503 = no idea about these.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

3 year bump.

Thanks to the reverse engineering efforts of abridgewater & Zorchenhimer over on Discord (abridgewater's original notes are here), I've managed to build a (far-from-perfect) decoder for the Study Box's data track. The data is encoded using MFM, and I've been able to emulate the mapper/drive enough to allow it to load pre-decoded data files and emulate them.

Unfortunately, the audio recordings for Study Box tapes I currently have in my possession appear to contain what looks like they might be glitches in the tapes, which my decoder cannot deal with properly, making a large portion of the "pages" stored on each tape harder to decode. At the moment, it's not certain whether these are recording glitches, or if the original tapes contain them (and if they do, how the Study Box deals with them is unknown.) The cuts/glitches in the data look like this: Image

The code for the very simplistic MFM decoder I wrote is here: https://github.com/SourMesen/MfmDecoder (it expects the input to be 44000hz 16-bit signed stereo wave files, with the data in the right channel)

Video/screenshots of what a couple different data sets look like:
Image

Image
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by Shonumi »

I've always found the Study Box to be a fascinating add-on for the Famicom. Great to see progress reverse-engineering and emulating it. Awesome work!
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by calima »

Just a guess from someone who just read the quick description of the thing: it has selectable "chapters" on each cassette, no? Perhaps that silence is the chapter separator. It can be found relatively easily.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

Ah, sorry, I should have been clearer. There are obvious separators for the chapter data - these usually last about 3 seconds, and I've been using them to split the data into multiple files. In contrast, these "gaps" in the data only last about 100ms and seem to come abruptly in the middle of a byte's data and then the other side of the gap suddenly continues the data without any preface similar to the one visible at the start of each "chapter" (which is a very long string of 0s following by a single 1 bit.)

It's also worth noting that I've only seen the gaps in the data track occur at points in the tape where the audio track is silent for a few seconds (at least so far, I haven't checked each tape in depth)
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

After working on this some more, it looks like the post-gap data eventually contains a "lead-in" portion like the one found at the start of a full-fledged "page"/"chapter". e.g it contains a long string of 0s followed by a single 1, after which the actual data begins. With this in mind, I was able to alter the decoder to be able to process this correctly and recover properly after the gaps. As is, I've been able to properly convert about 70% of the audio to actual data (though it's unfortunately hard/impossible to confirm whether or not the data is bit-perfect.) Some of the tape recordings are giving the decoder a bit of a hard time detecting the clocks in the signal, but that should be fixable with some tweaking. At least one of the ~30 tape recordings I have seems to have slightly distorted audio at the very beginning of the tape, so that data might be unrecoverable, though. But with some luck it should be possible to get 90+% of the audio data converted properly.

So far, I've managed to play through the entirety of the "Enjoy English 6" tape without any major glitches (conveniently found a hardware recording of most of that tape to compare against: https://www.youtube.com/watch?v=1q5C8Y6Znxk)

The mapper implementation is probably very inaccurate still, but it's working pretty decently at least.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

Just in time for the new decade, Study Box emulation in Mesen is now mostly complete (though some timings, etc. are still not accurate, games mostly appear to be behaving as expected.)
Here's what it looks like: https://www.youtube.com/watch?v=wqPWEv2yGFA

This required creating a new file format to hold the tape data (.studybox files.) This is a chunk format like RIFF/NSFe which contains both the tape's data (converted to binary data, not as audio data) and an embedded sound file for the audio track (could technically be any type of file, but mesen currently only supports 16-bit mono wav files.) The format was chosen because it removes the need of the audio data track (which removes 100+mb worth of WAV data per tape), makes it so that emulators don't need to incorporate an MFM decoder (which could easily be faulty) and doesn't require any complex code to load.

Essentially it works like FDS emulation. A "bios" file is required (this is the study box's PRG data) and then you can load .studybox files as if they were a ROM, which starts up the study box rom with that tape loaded.

So far, out of 36 audio tapes, I've managed to successfully convert 23 of them. Some of the other 13 recordings look like they have portions of the data that may be rather hard to salvage, but hopefully it'll be possible to convert those in due time, too. I'm unsure how many study box tapes actually exist, though (but given the names of the tapes I currently have, I would imagine there are at least 20+ more tapes that were made)
amatrix
Posts: 2
Joined: Sat Dec 16, 2023 4:24 am

Re: Study Box (iNES 186) Audio Tape Recording

Post by amatrix »

Another 3+1 year bump.

Is there any method to convert .studybox file back to tape record in audio format to write back to tape for test a real Studybox device?
Last edited by amatrix on Sat Dec 16, 2023 4:29 am, edited 1 time in total.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Study Box (iNES 186) Audio Tape Recording

Post by Sour »

amatrix wrote: Sat Dec 16, 2023 4:28 am Another 3+1 year bump.

Is there any method to convert .studybox file back to tape record in audio format to write back to tape for test a real Studybox device?
In theory, this is probably possible, it's "just" a matter of re-encoding the binary data as audio using the MFM encoding that the tapes use. The .studybox files should technically contain enough information to re-create the original audio data (or at least, something close enough that it should work) In practice, though, I don't think there's any tool to do this - someone would have to develop one. It's probably easier to find an original tape or tape recording if you're trying to test the hardware.
Post Reply