File size: 8,074 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node

import { setTimeout as pause } from "timers/promises";
import url from "node:url";
import fs from "node:fs";

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
const { V86 } = await import(TEST_RELEASE_BUILD ? "../../build/libv86.mjs" : "../../src/main.js");

process.on("unhandledRejection", exn => { throw exn; });

function regexp_escape(text)
{
    // TODO: The official RegExp.escape() would be prefarrable to this,
    // but currently (Aug 2025) the May 2025 Baseline is not yet available
    // at github CI.
    return text.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
}

async function exec_test(test_name, v86_config, timeout_sec, test_function)
{
    console.log("Starting: " + test_name);
    const tm_start = performance.now();
    const emulator = new V86(v86_config);
    const timeout = setTimeout(async () => {
        console.warn(emulator.screen_adapter.get_text_screen());
        await emulator.destroy();
        throw new Error("Timeout error in test " + test_name);
    }, timeout_sec * 1000);
    await new Promise(resolve => emulator.bus.register("emulator-started", () => resolve()));
    try
    {
        await test_function(emulator);
    }
    catch(err)
    {
        console.warn(emulator.screen_adapter.get_text_screen());
        throw new Error("Error in test " + test_name, { cause: err });
    }
    clearTimeout(timeout);
    await emulator.destroy();
    console.log("Done: " + test_name + " (" + ((performance.now() - tm_start) / 1000).toFixed(2) + " sec)");
}

/**
 * Execute given CLI command and wait for its completion.
 *
 * Injects command into the guest's keyboard buffer, then waits for both the
 * command and the expected response lines to show at the bottom of the VGA
 * screen.
 *
 * If command is empty then no command is executed and only the expected
 * response lines are waited for. If command contains only whitespace and/or
 * newline characters then it is send to the guest, but it does not become
 * part of the expected response.
 *
 * Allowed character set for command and expected is the printable subset
 * of 7-bit ASCII, use newline character "\n" to encode ENTER key.
 *
 * Throws an Error if the given timeout elapsed before the expected response
 * could be detected.
 *
 * @param {V86} emulator
 * @param {string} command
 * @param {Array<string|RegExp>} expected
 * @param {number} timeout_msec
 */
async function expect(emulator, command, expected, timeout_msec)
{
    if(command)
    {
        for(const ch of command)
        {
            emulator.keyboard_send_text(ch);
            await pause(10);
        }
        expected = [new RegExp(regexp_escape(command.trimRight()) + "$"), ...expected];
        await pause(100);
    }
    if(!await emulator.wait_until_vga_screen_contains(expected, {timeout_msec: timeout_msec}))
    {
        throw new Error("Timeout of " + timeout_msec + " msec expired");
    }
}

const CONFIG_MSDOS622_HD = {
    bios: { url: __dirname + "/../../bios/seabios.bin" },
    vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
    hda: { url: __dirname + "/../../images/msdos622.img" },
    autostart: true,
    memory_size: 32 * 1024 * 1024,
    log_level: 0,
    disable_jit: +process.env.DISABLE_JIT
};

const CONFIG_TINYCORE_CD = {
    bios: { url: __dirname + "/../../bios/seabios.bin" },
    vga_bios: { url: __dirname + "/../../bios/vgabios.bin" },
    cdrom: { url: __dirname + "/../../images/TinyCore-11.0.iso" },
    autostart: true,
    memory_size: 128 * 1024 * 1024,
    log_level: 0,
    disable_jit: +process.env.DISABLE_JIT
};

await exec_test("floppy-insert-eject", CONFIG_MSDOS622_HD, 60, async emulator =>
{
    console.log("Waiting for C:\\>");
    await expect(emulator, "", ["C:\\>"], 10000);

    console.log("Reading A:");
    await expect(emulator, "dir A:\n", ["", "", "General failure reading drive A", "Abort, Retry, Fail?"], 5000);
    await expect(emulator, "A", ["", "C:\\>"], 1000);

    console.log("Inserting disk freedos722.img into drive fda");
    await emulator.set_fda({ url: __dirname + "/../../images/freedos722.img" });

    console.log("Reading A:X86TEST.ASM");
    await expect(emulator, "dir /B A:X86TEST.ASM\n", ["X86TEST.ASM", "", "C:\\>"], 3000);

    console.log("Ejecting disk from drive A:");
    emulator.eject_fda();

    console.log("Reading A:");
    await expect(emulator, "dir A:\n", ["", " Volume in drive A is FREEDOS", "", "General failure reading drive A", "Abort, Retry, Fail?"], 5000);
});

await exec_test("floppy-insert-fdb", CONFIG_MSDOS622_HD, 60, async emulator =>
{
    console.log("Waiting for C:\\>");
    await expect(emulator, "", ["C:\\>"], 10000);

    console.log("Inserting disk freedos722.img into drive fdb");
    await emulator.set_fdb({ url: __dirname + "/../../images/freedos722.img" });

    console.log("Reading B:X86TEST.ASM");
    await expect(emulator, "dir /B B:X86TEST.ASM\n", ["X86TEST.ASM", "", "C:\\>"], 3000);

    console.log("Formatting B:");
    await expect(emulator, "format /V:V86 /U B:\n", ["Insert new diskette for drive B:", "and press ENTER when ready..."], 3000);
    await expect(emulator, "\n", [/Volume Serial Number is [0-9A-F-]+/, "", "Format another (Y/N)?"], 3000);
    await expect(emulator, "N\n", ["", "", "C:\\>"], 3000);
});

await exec_test("floppy-tinycore-linux", CONFIG_TINYCORE_CD, 60, async emulator =>
{
    console.log("Waiting for boot menu");
    await expect(emulator, "", [/BIOS default device boot in \d+ seconds\.\.\./], 10000);

    // press arrow down key 3 times
    for(let i = 0; i < 3; i++)
    {
        emulator.keyboard_send_scancodes([
            0xe0, 0x50,        // press
            0xe0, 0x50 | 0x80, // release
        ]);
        await pause(600);
    }

    console.log("Waiting for tc@box:~$");
    await expect(emulator, "\n", ["tc@box:~$"], 30000);

    console.log("Inserting disk windows101.img into drive fda");
    await emulator.set_fda({ url: __dirname + "/../../images/windows101.img" });

    console.log("Mounting /dev/fd0 into /mnt/fd0");
    await expect(emulator, "mkdir /mnt/fd0\n", ["tc@box:~$"], 3000);
    await expect(emulator, "sudo mount /dev/fd0 /mnt/fd0\n", ["tc@box:~$"], 3000);

    console.log("Reading /mnt/fd0/COMMAND.COM");
    await expect(emulator, "ls /mnt/fd0/COMMAND.COM\n", ["/mnt/fd0/COMMAND.COM", "tc@box:~$"], 3000);

    console.log("Unmounting fda");
    await expect(emulator, "sudo umount /dev/fd0\n", ["tc@box:~$"], 3000);

    console.log("Formatting /dev/fd0");
    await expect(emulator, "sudo mke2fs -q /dev/fd0\n", ["/dev/fd0 contains a vfat file system labelled 'WIN101'", "Proceed anyway? (y,N)"], 3000);
    await expect(emulator, "y\n", ["tc@box:~$"], 5000);

    console.log("Reading /mnt/fd0");
    await expect(emulator, "sudo mount /dev/fd0 /mnt/fd0\n", ["tc@box:~$"], 3000);
    await expect(emulator, "ls /mnt/fd0\n", ["lost+found/", "tc@box:~$"], 3000);
});

await exec_test("floppy-state-snapshot", CONFIG_MSDOS622_HD, 60, async emulator =>
{
    console.log("Waiting for C:\\>");
    await expect(emulator, "", ["C:\\>"], 10000);

    console.log("Inserting disk freedos722.img into drive fda");
    await emulator.set_fda({ url: __dirname + "/../../images/freedos722.img" });

    console.log("Saving initial state");
    const initial_state = await emulator.save_state();

    console.log("Creating file A:V86TEST.TXT");
    await expect(emulator, "echo v86test > A:V86TEST.TXT\n", ["", "C:\\>"], 3000);

    console.log("Saving modified state");
    const modified_state = await emulator.save_state();

    console.log("Restoring initial state");
    await emulator.restore_state(initial_state);

    console.log("Reading A:V86TEST.TXT");
    await expect(emulator, "dir /B A:V86TEST.TXT\n", ["", "C:\\>"], 3000);

    console.log("Restoring modified state");
    await emulator.restore_state(modified_state);

    console.log("Reading A:V86TEST.TXT");
    await expect(emulator, "dir /B A:V86TEST.TXT\n", ["V86TEST.TXT", "", "C:\\>"], 3000);
});