#!/usr/bin/perl -w
use warnings;
use strict;

# Width: 128 bit
#
# Mapping:
# 28 | 27 26 25 24 | 23 22 21 20 | 19 18 17 16 | 15 14 13 12 | 11 10 9  8 | 7  6  5  4 | 3  2  1  0
# H  | H  B  B  B  | R  R  R  R  | R  R  R  R  | R  R  R  R  | R  C  C  C | C  C  C  C | Y  Y  Y  Y


open(OUT, "> wideio.stl");

my $length = 100000;
my $size = 0x10;
my $channelOffset = 0x8000000;
my $state = 0; 
# state 0: reads only linear
# state 1: reads only random
# state 2: read and writes linear

for(my $i=0; $i < $length; $i += 4)
{
    my $r = 0;
   
    if($state == 0)
    {
        print OUT ($i+0).": read ".sprintf("0x%x",($size*$i)+0*$channelOffset)."\n";
        print OUT ($i+1).": read ".sprintf("0x%x",($size*$i)+1*$channelOffset)."\n";
        print OUT ($i+2).": read ".sprintf("0x%x",($size*$i)+2*$channelOffset)."\n";
        print OUT ($i+3).": read ".sprintf("0x%x",($size*$i)+3*$channelOffset)."\n";
    }
    elsif($state == 1)
    {
        $r = int(rand($channelOffset));
        print OUT ($i+0).": read ".sprintf("0x%x",($size*$r)+0*$channelOffset)."\n";
        $r = int(rand($channelOffset));
        print OUT ($i+1).": read ".sprintf("0x%x",($size*$r)+1*$channelOffset)."\n";
        $r = int(rand($channelOffset));
        print OUT ($i+2).": read ".sprintf("0x%x",($size*$r)+2*$channelOffset)."\n";
        $r = int(rand($channelOffset));
        print OUT ($i+3).": read ".sprintf("0x%x",($size*$r)+3*$channelOffset)."\n";
    }
    elsif($state == 2)
    {
        my $rw = int(rand(2))%2;
        if($rw == 0)
        {
            print OUT "$i: read ".sprintf("0x%x",($size*$i)+0*$channelOffset)."\n";
            print OUT "$i: read ".sprintf("0x%x",($size*$i)+1*$channelOffset)."\n";
            print OUT "$i: read ".sprintf("0x%x",($size*$i)+2*$channelOffset)."\n";
            print OUT "$i: read ".sprintf("0x%x",($size*$i)+3*$channelOffset)."\n";
        }
        else
        {
            print OUT "$i: write ".sprintf("0x%x",($size*$i)+0*$channelOffset)."\n";
            print OUT "$i: write ".sprintf("0x%x",($size*$i)+1*$channelOffset)."\n";
            print OUT "$i: write ".sprintf("0x%x",($size*$i)+2*$channelOffset)."\n";
            print OUT "$i: write ".sprintf("0x%x",($size*$i)+3*$channelOffset)."\n";
        }
    }
    else
    {
        print "Error generating traces (".$state.")";
        exit(-1);
    }

    if(($i != 0) && (($i % 1000) == 0))
    {
        # GOTO next state every 1000st request:
        $state = ($state + 1) % 3;
    }
}

close(OUT);
