Skip to content

Instantly share code, notes, and snippets.

@max1220
max1220 / monitor.bash
Created March 12, 2025 20:08
Bash performance monitor script. Outputs CSV data ready for gnuplot!
#!/bin/bash
set -euo pipefail
# change to log output directory
#cd /var/log/sys_stats
cd sys_stats
# wrap stdin with timestamp and newline
function wrap() { echo -n "${EPOCHREALTIME},"; cat; echo; }
@max1220
max1220 / build_and_run.sh
Created February 11, 2025 20:36
Bash script to create a Lua wrapper from Verilog using Verilator
#!/bin/bash
set -euo pipefail
# name of the top module(filename must match as well!)
TOP_MODULE="mcpu_top"
# Generate XML representation
echo -e "\e[33m --- Generating XML ---\e[0m"
verilator --xml-only --top "${TOP_MODULE}" "${TOP_MODULE}.v"
@max1220
max1220 / emulator_jit.t.lua
Created December 6, 2024 09:12
(BROKEN!) A simple JITed emulator for my MCPU instruction set(does not work yet! Will update soon)
local debug_print = true
-- index into the state array("optimized" for instruction decoding)
local STATE_PC = 0
local STATE_ADDR = 1
local STATE_ALU_A = 2
local STATE_IMM = 3
local STATE_ALU_B = 4
local STATE_I = 5
local STATE_J = 6
@max1220
max1220 / pretty_print_table.lua
Created April 11, 2024 05:15
Configurable Lua table ANSI pretty printer/serializer. Public domain.
local function pretty_print_table(tbl, file, indent, num_fmt, no_ansi, style, seen)
-- default parameters
file = file or io.stdout
indent = indent or 0
num_fmt = tbl.__print_table_number_format or num_fmt or "%.2f"
style = tbl.__print_table_colors or style or {}
seen = seen or {}
-- calculate ANSI color codes
local key_code = no_ansi and "" or style.key or "\027[1;36m"
@max1220
max1220 / ffi_defs_drm.lua
Created February 26, 2024 23:19
Basic LuaJIT FFI cdefs for using libdrm.
local ffi = require("ffi")
ffi.cdef([[
typedef unsigned int mode_t;
typedef unsigned int pid_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef unsigned int id_t;
typedef unsigned int dev_t;
/* from Linux/include/uapi/drm/drm.h */
@max1220
max1220 / hello-world-cpu.circ
Created April 24, 2023 12:42
A very simple LogiSim CPU
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project source="3.8.0" version="1.0">
This file is intended to be loaded by Logisim-evolution v3.8.0(https://github.com/logisim-evolution/).
<lib desc="#Wiring" name="0">
<tool name="Pin">
<a name="appearance" val="classic"/>
</tool>
</lib>
<lib desc="#Gates" name="1">
@max1220
max1220 / manage_containers.lua
Created March 9, 2021 14:14
Simple terminal GUI LXC container manager
#!/usr/bin/env lua5.1
--[[
This file provides a simple menu for managing LXC containers
by calling the associated lxc-* programms.
It uses the dialog util to provide interactive menus.
Currently, it supports:
* create/start/stop/delete/clone/rename containers
* list running/stopped containers
* show info/statistics about container
@max1220
max1220 / rc4.lua
Created March 5, 2021 16:55
Simple RC4 Stream Cipher implementation in Lua
-- small Lua RC4 library.
-- Tested in luajit and lua5.3.
-- Use for testing purposes only!
-- RC4 is broken, and this is not a well-tested implementation.
local ok,bit = pcall(require, "bit")
if not ok then
bit = require("bit32")
end
assert(bit and (bit.bxor(0xFF,0xF0)==0x0F)) -- test bit library xor
@max1220
max1220 / html_index.lua
Created May 11, 2020 04:07
Generate simple HTML directory indexes (recursive)!
#!/usr/bin/env lua
-- return stdout of command as a table containg lines
local function stdout_lines(cmd)
local proc = io.popen(cmd, "r")
local lines = {}
for line in proc:lines() do
lines[#lines+1] = line
end
proc:close()
return lines
@max1220
max1220 / config.lua
Created July 9, 2019 14:57
A Chrome(-ium) Bookmark downloader for youtube videos
return {
-- file extensions that are possibly a downloaded youtube video
file_extensions = {"mp3", "opus", "m4a", "ogg"},
-- directorys to scan for youtube id's
local_dirs = {
"/media/your/music/library/folder/that/contains/youtube_dl/downloaded/videos/"
},
-- where to store the downloaded files