File class for VMEmulator

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

File class for VMEmulator

cadet1620
Administrator
This post was updated on .
[14 Jan 2015 -- Updated with version 1.1.]

Wouldn't it be sweet to be able to read and write data on your host computer from a program running on the VM Emulator?

Imagine your Jack game being able to keep a persistent High Score list! I want to try writing a Jack compiler in Jack.

This post presents class File, a Jack class that does file I/O on the host computer's file system. Here's a screen shot of my High Score test application and a dump of the score file stored on my hard disk.

high score test screen shot

file dump screen shot

The File class supplies basic open/read/write/close access to the disk. To give you a quick feel for how it works, here's what The HighScore.save() function looks like this.

/**
 *  Save this HighScore object in a File.
 *
 *  Returns false if the file write failed.
 *
 *  File format: int:count int:maxNameLen ( int:score str:name \n ) *
 */
    method boolean save (String filename) {
        var int i, j;
        var File file;
        var String s;

        let file = File.new();
        if (~file.open(filename, true)) {
            do file.dispose();
            return false;
        }
        // Write entry count.
        if (~file.writeInt(count)) {
            do file.dispose();
            return false;
        }
        // Write max name length.
        let i = 0;
        let j = 0;
        while (i < count) {
            let s = names[i];
            if (s.length() > j) {
                let j = s.length();
            }
            let i = i+1;
        }
        if (~file.writeInt(j)) {
            do file.dispose();
            return false;
        }
        // Write entries.
        let i = 0;
        while (i < count) {
            if (~file.writeInt(scores[i])) {
                do file.dispose();
                return false;
            }
            if (~file.writeString(names[i])) {
                do file.dispose();
                return false;
            }
            if (~file.writeln()) {
                do file.dispose();
                return false;
            }
            let i = i+1;
        }
        do file.dispose();
        return true;
    }
The documentation's still a bit sparse: File-class.html

By default, the File class can can only access files in and below the nand2tetris/tools/VmeFileSystem directory so you can't clobber your disk with a buggy Jack program. If you want to live dangerously, you can move the root of the VME's filesystem. See VMEFSROOT in the doc.

Here's the built-in class for the VME and the test code
  File-class-1-1.zip

Tested on Windows XP + java version 1.7.0_71, Windows 7 + java version 1.8.0_25, and Ubuntu Linux 12.04 + OpenJDK version 1.6.0_33.

Many thanks to ivant for providing an easy to build source tree. All I had to do was add my File.java source in the appropriate build directory. [And learn some Java; the only Java program I'd ever written was the obligatory hello.java!]

Enjoy,
--Mark

Reply | Threaded
Open this post in threaded view
|

Re: File class for VMEmulator (version 1.1)

cadet1620
Administrator
Fixed a bug that was leaving host files locked when programs using the File class where reset or reloaded while files were open.

File-class-1-1.zip

--Mark