Skip to content

Instantly share code, notes, and snippets.

@thentenaar
thentenaar / ti-prng.c
Created November 25, 2012 01:12
Implementation of TI Basic's PRNG
/**
* Implementation of the TI BASIC PRNG
*
* Copyright (C) 2009, Tim Hentenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
@thentenaar
thentenaar / gist:4142008
Created November 25, 2012 01:18
Disassembly of the TI's GPL RAND command
RAND:
LI R4,>6FE5 ; R4 = >6fe5
MPY @>83C0,R4 ; R4,R5 = R4 × @>83C0 (initially >3567 when BASIC loads)
AI R5,>7AB9 ; R5 += >7ab9
MOV *R5,@>83C0 ; R5 -> @>83C0
MOVB R13,R6 ; 'm' (>64 when called from BASIC) -> R6
SRL R6,8 ; R6 & 0xff
INC R6 ; R6++ (prevent division by zero)
CLR R4 ; R4 = 0
@thentenaar
thentenaar / gist:4142031
Created November 25, 2012 01:30
Disassembly of TI BASIC's RND function
4F00: ST @>834A,>3F ; Zero loop counter: >3F (63)
4F03: ST @>8310,>4B ; Number buffer address: >834B
4F06: RAND >63 ; Get a pseudo-random number (using 100 as the divisor)
4F08: CZ @>8378 ; If then number isn't zero, goto >4F16
4F0A: BR >4F16
4F0C: DEC @>834A ; Decrement the zero loop counter
4F0E: CZ @>834A ; If the zero loop counter is 0, goto >4F23 (return 0)
4F10: BS >4F23
4F12: BR >4F06 ; Otherwise, continue the loop
4F14: RAND >63 ; Get the next pseudo-random number
@thentenaar
thentenaar / gist:4143575
Created November 25, 2012 13:49
C Implementation of the TI's GPL RAND function
static uint16_t seed;
/* RAND: Generate an 8-bit pseudo-random number */
uint8_t gpl_rand(uint8_t divisor) {
/* Update the seed */
seed = ((uint32_t)(seed * 0x6fe5) & 0xffff) + 0x7ab9;
/* Swap bytes, and compute modulus */
return ((((seed & 0xff00) >> 8 ) | ((seed & 0x00ff) << 8 )) % (!divisor ? 1 : divisor));
}
@thentenaar
thentenaar / gist:4143585
Created November 25, 2012 13:52
C Implementatation of TI BASIC's RND function
/* Equivalent to PRINT RND in TI Basic */
char *rnd() {
uint8_t rnds[7]; int i; char *n;
/* Try up to 64 times to come up with a non-zero pseudo-random integer */
for (i=0;i<63;i++) if ((rnds[0] = gpl_rand(100)) != 0) break;
if (rnds[0] == 0) return strdup("0");
/* Generate the rest of the set */
for (i=1;i<7;i++) rnds[i] = gpl_rand(100);
@thentenaar
thentenaar / gist:5227554
Last active September 12, 2017 14:16
Script for controlling the backlight in the ASUS G74SX
#!/bin/bash
#
# Brightness script for the ASUS G74SX
#
# Arguments:
# up - increase brightness by 1
# down - decrease brightness by 1
#
BRIGHTNESS=`cat /sys/devices/platform/asus-nb-wmi/leds/asus\:\:kbd_backlight/brightness`
@thentenaar
thentenaar / geos6fix.asm
Last active August 31, 2021 09:41
DOS 6+ Fix for PC/GEOS 1.x (and an example of an extremely simply file patcher.)
;
; DOS 6+ Fix for PC/GEOS 1.x
; (and an example of an extremely simply file patcher)
;
; Copyright (C) 2013 Tim Hentenaar, http://hentenaar.com
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
@thentenaar
thentenaar / add_to_calcurse.sh
Last active April 8, 2020 20:14
A simple script for importing text/calendar files into calcurse
#!/bin/bash
#
# Import text/calendar files from mutt
# to calcurse.
#
# Make sure calcurse is running
if [ ! -f "$HOME/.calcurse/.calcurse.pid" ]; then
exit 1
fi
@thentenaar
thentenaar / echo-server.c
Created January 26, 2015 20:55
A very simple, and quickly implemented, echo server
/**
* Simple echo server
*
* This program listens for connections on the
* loopback interface, on port 9999. Upon connecting,
* The first line we read, of up to BUF_LEN bytes, is
* then sent back to the sender, and the connection
* is closed.
*/
#include <stdio.h>
@thentenaar
thentenaar / echo-server-1.c
Created January 26, 2015 20:59
Simple echo server: Refactor #1: Move the socket setup code into its own function.
/**
* Simple echo server
*
* This program listens for connections on the
* loopback interface, on port 9999. Upon connecting,
* The first line we read, of up to BUF_LEN bytes, is
* then sent back to the sender, and the connection
* is closed.
*
* Refactor #1 - Move socket creation code into its own