Other articles

  1. ATmega32 Bare Metal JTAG - Part 1: External Interrupt Handling and TCK

    This example expands on the code from previous examples running code on the ATmega32. Note that this code is building towards interrogating other devices using JTAG, not demonstrating the internal JTAG features of the ATmega32 specifically.

    I started by cleaning up the driver for the status LED on PORTB0. This …

    read more
  2. Resistor Quiz

    This is a simple resistor quiz for both color bands to values and values to color bands. I wrote it because I couldn't find a quiz generator which worked in both directions. All values are in ohms. The list common_values can be altered to change the list of values from …

    read more
  3. Toy Genetic Algorithm, Part 2

    This builds on the last example by using the simulate function of a Circuit to drive a circuit simulator (Ahkab) with a Population of random Circuits made of random Components (Resistor, Inductor. and Capacitor) representing low-pass filters. It runs an AC analysis and minimizes the maximum attenuation in …

    read more
  4. Lock-Free Multiprocess Shared Memory Counter (Atomic CAS) in C

    This C code forks a number of processes (children) which each contribute a per_child number of increments to a globally shared counter. The parent process does not wait for the children to exit. Each child sleeps for a random number of nanoseconds between reading the shared counter and attemping to …

    read more
  5. ATmega32 Bare Metal Boot

    Bare metal boot in AVR assembly without avr-libc. Demonstrates interrupt vectors, enabling the stack, the watchdog timer, and blinking an LED connected to PORTB, pin 0.

    On boot, the LED will cycle three times quickly, then begin blinking. After approximately 2.2 seconds, it will be reset by the Watchdog …

    read more
  6. Simple XOR Hash

    NOTE: I originally posted this on Snipplr.

    This is a simple hash that pads its input to the block size and XORs every block together. Output is in hexadecimal octets. Probability of collisions is extremely high and they are easy to calculate, although the function is one-way, so this is …

    read more
  7. ARC4

    NOTE: I originally posted this on Snipplr.

    This is a pure Python implementation of ARC4 as a generator to highlight its nature as a stream cipher. Several improvements can be made, for instance, it could take a nonce, use multiple state spaces (parallelizable), automatically discard the first 4K of the …

    read more
  8. HMAC

    NOTE: I originally posted this on Snipplr.

    To HMAC, pass a hash from Crypto.Hash in PyCrypto. Key should be a bytes object. Returns a bytearray.

    def pad_key(a_hash, a_key):
        block_size = a_hash.digest_size
        if (len(a_key) > block_size):
            a_hasher = a_hash.new()
            return a_hasher.digest()
        elif (len(a_key) < block_size …
    read more
  9. Chunk Data For Streaming

    NOTE: I originally posted this on Snipplr.

    Chunks data into block_size blocks for streaming, adds null padding.

    import math
    is_debug = False
    def chunk(data, block_size, padding=b'\x00'):
        data_size       = len(data)
        padding_size    = block_size - (data_size % block_size) if (data_size > block_size) else (block_size - data_size)
        is_padded       = (padding_size > 0) and (padding_size != block_size)
        total_blocks    = math.ceil …
    read more
  10. k-d Tree

    NOTE: I originally posted this on Snipplr.

    make-kd-node [median left right]: Creates a node in a kd-tree.

    make-kd-tree [k depth points]: Creates a kd-tree of kd-nodes. TODO: Not stack safe. Use loop/recur.

    kd-nearest-neighbor [a-point kd-tree]: Returns the nearest neighboring point to a given point, using a kd-tree.

    (ns kd-tree …
    read more
  11. Axis-Aligned Bounding Box

    NOTE: I originally posted this on Snipplr.

    make-aabb [a-point b-point] [a-point b-point]: Creates a 3D Axis-Aligned Bounding Box (AABB).

    aabb-contains? [a-box a-point]: Returns true if a 3D AABB contains a given point.

    (ns aabb)
    (defn ^{
        :doc "Creates a 3D Axis-Aligned Bounding Box (AABB)."
    } make-aabb [a-point b-point] [a-point b-point])
    (defn ^{
        :doc …
    read more