#!/usr/bin/env python3

import struct

(TYPE_NOP, TYPE_READ, TYPE_WRITE, TYPE_END) = range(4)

# Write n as unsigned int in big endian format to file f.
def write32(n):
    f.write(struct.pack('>I', n))

# Combine type and address and write to file.
def entry(t, addr):
    write32(t | (addr & ~3))

# Open trace file as f and write header information.
def openTrace(filename, num_proc):
	global f
	f = open(filename, "wb")
	f.write(b"2TRF") # trace file signature.
	write32(num_proc)

# Open a tracefile called <this_python_file_name>.trf
openTrace(__file__.replace('.py', '.trf'), 2)

# 2 processor trace, so generate pairs of events for P0 and P1.

# Read some data, from p0 and p1
# Should both end up in shared state.
entry(TYPE_READ, 0x100)    # P0
entry(TYPE_READ, 0x100)    # P1

# Do nothing.
for i in range(200):
    entry(TYPE_NOP, 0x0)
    entry(TYPE_NOP, 0x0)

# End of trace
entry(TYPE_END, 0x0)
entry(TYPE_END, 0x0)

f.close()
