Skip to content

Commit

Permalink
Uses virtio-console instead of serial for better performance (and aut…
Browse files Browse the repository at this point in the history
…o resize in the future)
  • Loading branch information
alexkar598 committed Jun 23, 2024
1 parent a41b280 commit ba63d05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
19 changes: 11 additions & 8 deletions src/vm/commandQueue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ export class CommandQueueService {
public constructor(public emulator: EmulatorService) {
//It's called from window.setTimeout so this is window otherwise
this.tickQueue = this.tickQueue.bind(this);
emulator.receivedOutputController.subscribe((chr) => {
try {
this.receiveChr(chr);
} catch (error) {
const errorMsg = `The command queue has encountered an error. Please report the following message and reload the page: ${error}`;
console.log(errorMsg, error);
alert(errorMsg);
this.suspendQueue();
emulator.receivedOutputController.subscribe((data) => {
for (const chr of data) {
//TODO: Rewrite the receive function to receive chunks instead of character by character
try {
this.receiveChr(chr);
} catch (error) {
const errorMsg = `The command queue has encountered an error. Please report the following message and reload the page: ${error}`;
console.log(errorMsg, error);
alert(errorMsg);
this.suspendQueue();
}
}
});
}
Expand Down
41 changes: 23 additions & 18 deletions src/vm/emulator.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type WorkerEventResponseMsg =
data: void;
};

const decoder = new TextDecoder();
const parameters = new URLSearchParams(location.search);
const emulator = new V86({
//Emulator binaries
Expand Down Expand Up @@ -148,24 +149,31 @@ const emulator = new V86({
//Loads bzimage and initrd from 9p filesystem
bzimage_initrd_from_filesystem: false,
autostart: true,
virtio_console: true,
});

//@ts-ignore
self.emulator = emulator;

let resetting = false;

function sendTerminal(message: string) {
for (let i = 0; i < message.length; i++) {
emulator.bus.send('serial0-input', message.charCodeAt(i));
}
emulator.bus.send(
'virtio-console0-input-bytes',
[...message].map((x) => x.charCodeAt(0)),
);
}
function sendScreen(message: string) {
for (let i = 0; i < message.length; i++) {
emulator.bus.send('serial1-input', message.charCodeAt(i));
}
emulator.bus.send(
'virtio-console1-input-bytes',
[...message].map((x) => x.charCodeAt(0)),
);
}
function sendController(message: string) {
for (let i = 0; i < message.length; i++) {
emulator.bus.send('serial2-input', message.charCodeAt(i));
}
emulator.bus.send(
'virtio-console2-input-bytes',
[...message].map((x) => x.charCodeAt(0)),
);
}
onmessage = ({ data: e }: MessageEvent<WorkerMsg>) => {
switch (e.command) {
Expand Down Expand Up @@ -230,24 +238,21 @@ onmessage = ({ data: e }: MessageEvent<WorkerMsg>) => {
}
};

emulator.add_listener('serial0-output-byte', (byte: number) => {
if (byte == 255) return;
emulator.add_listener('virtio-console0-output-bytes', (bytes: Uint8Array) => {
postMessage({
event: 'receivedOutputConsole',
data: [String.fromCharCode(byte)],
data: [decoder.decode(bytes)],
});
});
emulator.add_listener('serial1-output-byte', (byte: number) => {
if (byte == 255) return;
emulator.add_listener('virtio-console1-output-bytes', (bytes: Uint8Array) => {
postMessage({
event: 'receivedOutputScreen',
data: [String.fromCharCode(byte)],
data: [decoder.decode(bytes)],
});
});
emulator.add_listener('serial2-output-byte', (byte: number) => {
if (byte == 255) return;
emulator.add_listener('virtio-console2-output-bytes', (bytes: Uint8Array) => {
postMessage({
event: 'receivedOutputController',
data: [String.fromCharCode(byte)],
data: [decoder.decode(bytes)],
});
});

0 comments on commit ba63d05

Please sign in to comment.