Commit cc8d9855 authored by Arthur Carlsson's avatar Arthur Carlsson

Added ws crate and added some code to get started

parent fdf56235
Pipeline #26126735 passed with stage
in 3 minutes and 6 seconds
......@@ -7,3 +7,4 @@ authors = ["Arthur Carlsson <arthur@kiron.net>"]
clap = "2"
byteorder = "1.2"
bitflags = "1.0"
ws = "*"
use std::str;
use byteorder;
use byteorder::ByteOrder;
use std::marker::PhantomData;
use std::str;
/// Marker trait for a read-only memory
pub trait ReadOnly {
......@@ -64,7 +64,8 @@ impl<T> Mem<T> {
}
pub fn read_ascii_string(&self, addr: u32, count: usize) -> &str {
str::from_utf8(&self.buf[addr as usize..addr as usize + count]).unwrap()
str::from_utf8(&self.buf[addr as usize..addr as usize + count])
.unwrap()
.trim_right_matches('\0')
}
}
......
use std::thread;
use ws::{listen, Handler, Message, Request, Response, Result, Sender};
use std::sync::mpsc::{self, channel};
// This can be read from a file
static INDEX_HTML: &'static [u8] = br#"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="app.css" >
</head>
<body>
<pre id="messages"></pre>
<form id="form">
<input type="text" id="msg">
<input type="submit" value="Send">
</form>
<script src="app.js"></script>
</body>
</html>
"#;
static APP_CSS: &'static [u8] = br#"
"#;
static APP_JS: &'static [u8] = br#"
var socket = new WebSocket("ws://" + window.location.host + "/ws");
socket.onmessage = function (event) {
var messages = document.getElementById("messages");
messages.append(event.data + "\n");
};
var form = document.getElementById("form");
form.addEventListener('submit', function (event) {
event.preventDefault();
var input = document.getElementById("msg");
socket.send(input.value);
input.value = "";
});
"#;
struct Server {
out: Sender,
tx: mpsc::Sender<u32>
}
pub struct DebugSession {
rx: mpsc::Receiver<u32>
}
impl Handler for Server {
//
fn on_request(&mut self, req: &Request) -> Result<(Response)> {
// Using multiple handlers is better (see router example)
match req.resource() {
// The default trait implementation
"/ws" => Response::from_request(req),
// Create a custom response
"/" => Ok(Response::new(200, "OK", INDEX_HTML.to_vec())),
"/app.css" => Ok(Response::new(200, "OK", APP_CSS.to_vec())),
"/app.js" => Ok(Response::new(200, "OK", APP_JS.to_vec())),
_ => Ok(Response::new(404, "Not Found", b"404 - Not Found".to_vec())),
}
}
// Handle messages recieved in the websocket (in this case, only on /ws)
fn on_message(&mut self, msg: Message) -> Result<()> {
// Broadcast to all connections
self.out.broadcast(msg)
}
}
pub fn start() -> DebugSession {
let (tx, rx) = channel();
thread::spawn(move || {
listen("127.0.0.1:8000", |out| Server { out, tx: tx.clone() }).unwrap();
});
DebugSession { rx }
}
extern crate byteorder;
#[macro_use]
extern crate bitflags;
extern crate ws;
mod cart;
mod cpu;
pub mod debugger;
mod n64;
mod pif_rom;
mod rsp;
......
......@@ -29,20 +29,30 @@ fn main() {
.required(true)
.index(1),
)
.arg(
Arg::with_name("DEBUGGER")
.help("Start with debugger")
.long("debugger"),
)
.get_matches();
let pif = read_bin(matches.value_of("PIF").unwrap());
let rom = read_bin(matches.value_of("ROM").unwrap());
let debugger = matches.is_present("DEBUGGER");
let pif_rom = PifRom::new(pif);
let cart = Cart::new(rom);
println!("Running {}", cart.title());
if debugger {
let session = rustn64::debugger::start();
}
let mut n64 = N64::new(pif_rom);
n64.insert_cart(cart);
n64.power_on();
n64.start()
n64.start();
}
fn read_bin<P: AsRef<Path>>(path: P) -> Vec<u8> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment