Skip to content

Instantly share code, notes, and snippets.

@sean3z
sean3z / cpu.rs
Last active December 30, 2022 01:29
Example nibble parsing for Chip-8 emulator
pub fn emulate_cycle(&mut self) {
self.fetch_opcode();
self.execute_opcode();
// okay, PCs are much faster these days
// threw this cheeky delay in to slow things down
thread::sleep(time::Duration::from_micros(500));
}
fn fetch_opcode(&mut self) {
@sean3z
sean3z / cpu.rs
Last active December 30, 2022 01:01
Example throwing Fonts into our cpu.memory
impl Cpu {
pub fn new() -> Cpu {
let mut memory = [0; 4096];
// load fonts into memory
for i in 0..80 {
memory[i] = FONTS[i];
};
Cpu {
@sean3z
sean3z / cpu.rs
Last active December 30, 2022 00:48
Example loading ROM data into memory
pub fn load_game(&mut self, game: &str) {
// attempt to load supplied ROM
let mut reader = File::open(game).expect("Unable to locate ROM");
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).expect("Unable to read ROM data");
// load ROM into memory (AFTER system reserved memory)
for i in 0..buffer.len() {
self.memory[i + self.program] = buffer[i];
};
@sean3z
sean3z / cpu.rs
Last active December 30, 2022 00:36
Example CPU module
use rand;
use std::fs::File;
use display::Display;
use keypad::Keypad;
pub struct Cpu {
program: usize, // program counter starts at 512 bytes
opcode: u16, // current opcode
stack: [u16; 16], // stack storage
@sean3z
sean3z / main.rs
Last active December 30, 2022 00:27
Example main
use cpu::Cpu;
mod cpu;
mod keypad;
mod display;
fn main() {
let mut cpu = Cpu::new();
cpu.load_game("/home/sean/www/chip8-emulator-rust/roms/pong");
@sean3z
sean3z / apgar.mrc
Created December 29, 2022 23:30
WOL Apgar routine in mIRC Script (Frank Razenberg)
alias apgar {
var %lookup = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
var %out, %i = 1
while (%i <= 8) {
var %left, %right = 0
if (%i <= $len($1-)) { %left = $asc($mid($1-, %i ,1)) }
if ($calc($len($1-) - %i + 1) < $len($1-)) { %right = $asc($mid($1-, $calc($len($1-) - %i + 2), 1)) }
var %x = $iif(%left & 1, $and($calc(%left * (2 ^ $and(%left, 1))), %right), $xor(%left, %right))
;# echo -a left: %left - righT: %right - x: %x
%out = %out $+ $mid(%lookup, $calc($and(%x, 63) + 1), 1)
@sean3z
sean3z / apgar.cpp
Created December 29, 2022 23:29
WOL Apgar routine in CPP (RenegadeProjects.com)
std::string apgar(std::string input) {
std::string lookup = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::string out = "";
int i = 0;
while (i < 8) {
unsigned char left = input[i];
unsigned char right = input[input.length() - i];
unsigned char x = left & 1 ? ((left << 1) ^ (left & 1)) & right : left ^ right;
@sean3z
sean3z / apgar.js
Created December 29, 2022 23:27
WOL Apgar routine in Javascript
function apgar(s) {
if (s.length != 8) return false;
var s = s.split(''), o = [];
var u = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./';
for(var i = 0; i <= 7; i++) {
var a = s[(8 - i)], r = ((i == 0) ? 0 : a.charCodeAt(0));
var j = s[i].charCodeAt(0), x = ((j & 1) ? (j << 1) & r : j ^ r);
var k = (x & 0x3f), f = u.substring(k, (k + 1));
o.push(f);
}
@sean3z
sean3z / apgar.php
Created December 29, 2022 23:25
WOL Apgar routine in PHP
<?php
echo apgar('password'); //result WaIMMsbf
function apgar($a) {
if (strlen($a) == 8) {
$u = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for ($i = 0; $i <= 7; $i++) {
$r = ($i == 0) ? 0 : ord($a[8 - $i]);
$x = (ord($a[$i]) & 1) ? (ord($a[$i]) << 1) & $r : ord($a[$i]) ^ $r;
$o[] = substr($u, ($x & 0x3f), 1);
@sean3z
sean3z / pagination.php
Last active May 10, 2022 19:09
Pagination example
<?php
function handle_pagination($total, $page, $shown, $url) {
$pages = ceil( $total / $shown );
$range_start = ( ($page >= 5) ? ($page - 3) : 1 );
$range_end = ( (($page + 5) > $pages ) ? $pages : ($page + 5) );
if ( $page >= 1 ) {
$r[] = '<span><a href="'. $url .'">&laquo; first</a></span>';
$r[] = '<span><a href="'. $url . ( $page - 1 ) .'">&lsaquo; previous</a></span>';
$r[] = ( ($range_start > 1) ? ' ... ' : '' );