MyHDL: First LED Blinker Test Working!

I’ve been tinkering with MyHDL lately on the Z-turn board by MYIR. I have to say this board is pretty fun to play with so far, and i’ve left some resources on my blog about it, that might get updated later: MYIR Z-turn FPGA Board Pin Assignments.

Back to the MyHDL. I’m only just starting Python, but from the looks of it, it’s been fairly painless to understand so far. The use of Python (MyHDL is in fact only a library that’s used to do hardware dev and simulation) makes it faaaar easier to simulate designs than using the very frustrating Verilog simulators out there.

With the RTL logic, the rest of the verification framework can be regular python. Run the project, and you get a neat GTKWave output file, load it in, check if everything is okay, iterate.

I’ve written a little LED blinker for the Z-turn. It’s not the most amazing project ever, but it’s good as a template perhaps.

from myhdl import *

## Not synthesizable, it is used for the bench.
##
@block
def ClkDriver(clk, period=20):
	
	lowTime = int(period/2)
	highTime = period - lowTime

	@instance
	def drive_clk():
		while True:
			yield delay(lowTime)
			clk.next = 1
			yield delay(highTime)
			clk.next = 0

	return drive_clk

@block
def hello2_top(clk, reset_n, ledRed, ledGreen, ledBlue,
		led_period):

	"""
	clk: input, clock signal
	reset_n: input, reset signal, inverted.
	ledRed: output, signal inverted.
	ledGreen: output, signal inverted.
	ledBlue: output, signal inverted.
	"""

	count = Signal(intbv(0, 0, 2**64))

	@always (clk.posedge)
	def led_driver():
		if reset_n == 0:
			ledRed.next = not 0
			ledGreen.next = not 0
			ledBlue.next = not 0
		else:
			next_count = count + 1
			if next_count == led_period:
				next_count = 0
				ledRed.next = not ledRed
			count.next = next_count

	return instances()

@block
def hello2():

	clk1 = Signal(bool(0))

	ledR = Signal(bool(0))
	ledG = Signal(bool(0))
	ledB = Signal(bool(0))

	reset_n = Signal(bool(0))

	clkdriver_1 = ClkDriver(clk1)
	hello2_top_1 = hello2_top(clk1, reset_n, ledR, ledG, ledB, 1000)

	@instance
	def stimul():
		reset_n.next = 0
		yield delay(100)
		reset_n.next = 1

	return instances()

inst = hello2()
inst.config_sim(trace=True)
inst.run_sim(100000)

#
# Rig up for synthesis.
#

clk1 = Signal(bool(0))
reset_n = Signal(bool(0))
ledRed = Signal(bool(0))
ledGreen = Signal(bool(0))
ledBlue = Signal(bool(0))

inst = hello2_top(clk1,reset_n,ledRed,ledGreen,ledBlue, 12000000)
inst.convert(hdl="Verilog")

Running project looks like a board with a blinking LED.

Using ExpressVPN on Gentoo: Short Guide

ExpressVPN comes as either a deb or rpm package. We’ll choose the rpm package, so first install that.

sudo emerge rpm

Then you want to install the package without any dependencies.

sudo rpm -i --nodeps expressvpn-1.4.0-1.x86_64.rpm

ExpressVPN is now installed, but since Gentoo runs the OpenRC init system we’ll want to go ahead and create a service file in /etc/init.d/expressvpnd.

#!/sbin/openrc-run
command=/usr/sbin/expressvpnd
command_args=""
pidfile=/var/run/expressvpnd.pid
name="ExpressVPN Daemon"
command_background="yes"

depend() {
        # need net
        :
}

We need some more configuring.

ln -s /bin/ip /sbin/ip; modprobe tun

Then you can start the service as usual and add it to your runlevel default, and control the application using the Firefox extension available on the main website.

Vertcoin Basics: Computing Merkle Root Values

Looking at Crypto coins block explorers, I was wondering in the case of altcoins if the Merkle Root field is still computed the same way as in Bitcoin.

There’s already a lot of information on the general theory behind a Merkle Tree, so please refer to external sources for a better introduction. Here is instead a small worked example of how this applies to Bitcoin and as we will see, also Vertcoin.

QByteArray sha256d(QByteArray in)
{
QCryptographicHash hash (QCryptographicHash::Sha256);

hash.addData(in);

QByteArray res1 = hash.result();
hash.reset();
hash.addData(res1);

return hash.result();
}

This simple function computes the SHA256d hash of a byte array, as used everywhere in Bitcoin.

For the simple case where Tx count = 1, the Merkle root is simply the hash of the first transaction.

For a block with more transactions, let’s illustrate.

vertblock800019

a = 32e60eb21632597c1c1545c63fae57df8d4455648b7d97150b7dcee706749588
b = 1deed158ada568b9b193473095c8eb2cef2cffe60ccce15c3c82f599a09767a8

However there’s a bug with taking these values directly. It turns out Satoshi decided to flip the hash values for whatever arbitrary reason. We’ll need to unflip them to the correct endianness for our trees to grow correctly.

QByteArray reverse_hash (QByteArray in)
{
QByteArray res;
if (in.length() != 32) return QByteArray::fromHex(“”);

for (int i = 0; i < 32; ++i)
{
res.append(in[31 – i]);
}

return res;
}

Applying this, we now get:

a’ = 88957406e7ce7d0b15977d8b6455448ddf57ae3fc645151c7c593216b20ee632
b’ = a86797a099f5823c5ce1cc0ce6ff2cef2cebc895304793b1b968a5ad58d1ee1d

Then from theory we know that in our case, Root = Hash ( A | B).

Compute this, flip the bytes again, and you get the correct Merkle root as displayed in the explorer.

QByteArray c = a + b;
QByteArray m = sha256d( c );
m = reverse_hash(m);

For blocks with more transactions, repeat this process by merging more pairs.

Conclusion

The same process is used for both Bitcoin and Vertcoin, as well as probably a lot of other alts. Displayed hash values are byte-flipped around.

Since WordPress is not very good at displaying inline C++, here is the complete code example.

Code Golfing a “Is Power of Two?” Function

Instead of looping around, there’s a simpler way.

diff --git a/kernel/elf.cpp b/kernel/elf.cpp
index 8bee7c5..ca2d50c 100644
--- a/kernel/elf.cpp
+++ b/kernel/elf.cpp
@@ -42,12 +42,9 @@
namespace Sortix {
namespace ELF {

-static bool is_power_of_two(uintptr_t value)
+static bool is_power_of_two(uintptr_t x)
{
- for ( uintptr_t i = 0; i < sizeof(uintptr_t) * 8; i++ )
- if ( (uintptr_t) 1 << i == value )
- return true;
- return false;
+ return (x != 0) && ((x & (x - 1)) == 0);
}

The code in question is found in the excellent hobby OS sortix.

macOS – Running Steam Games on a Case–Sensitive Machine

Unfortunately, when you try running certain games on a mac, on a case sensistive HFS+ filesystem, you might hit a roadblock.

Steam itself will run fine.

Fortunately there is a solution by using OpenZFS. I won’t go into the details of installing ZFS on a mac as there is plenty of information out there. I simply use homebrew for that kind of thing. Refer to the OpenZFS Wiki, they have instructions on how to install using homebrew.

Method for Creating the Filesystem Image

OpenZFS supports creating a pool inside a file, so this is what we are going to do.

mkfile -n 5g steam_disk.zfs
zpool create -O casesensitivity=insensitive steam_pool /Users/$USER/steam_disk.zfs

Then you will get a shiny new volume with the required properties under /Volumes/steam_pool/. Fire up Steam, tell it to install there.

Enjoy, and don’t forget to read up on ZFS basics if you don’t know how to export and import a pool.

zpool export steam_pool
zpool import -d /Users/$USER/ -a