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

# Assuming this address mapping:
#   <addressmapping>
#        <channel from="27" to="28" />
#        <row   from="14" to="26" />
#        <column from="7" to="13" />
#        <bank  from="4" to="6" />
#        <bytes from="0" to="3" />
#   </addressmapping>

# This is how it should look like later:
# 31:     write   0x0     ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

my $numberOfRows = 8192;
my $numberOfColumnsPerRow = 128;
my $bytesPerColumn = 16;
my $burstLength = 4; # burst length of 4 columns --> 4 columns written or read per access
my $dataLength = $bytesPerColumn * $burstLength;

my $rowOffset = 0x4000;
my $colOffset = 0x80;

# Generate Data Pattern:
my $dataPatternByte = "ff";

my $dataPattern = "";
for(my $i = 0; $i < $dataLength; $i++)
{
    $dataPattern .= $dataPatternByte;
}

my $clkCounter = 0;
my $addr = 0;

# Generate Trace file (writes):

for(my $ch = 0; $ch < 4; $ch++) {
	$addr = 0;
	$addr |= $ch << 27;
	for(my $row = 0; $row < ($numberOfRows * $rowOffset); $row = $row + $rowOffset)
	{
		for(my $col = 0; $col < ($numberOfColumnsPerRow * $colOffset); $col = $col + ($colOffset * $burstLength))
		{
			my $addrHex = sprintf("0x%x", $addr);
			print "$clkCounter:\twrite\t$addrHex\t$dataPattern\n";
			$clkCounter++;
			$addr += $colOffset * $burstLength;
		}
	}
}

$clkCounter = 350000000;
$addr = 0;

# Generate Trace file (reads):
for(my $ch = 0; $ch < 4; $ch++) {
	$addr = 0;
	$addr |= $ch << 27;
	for(my $row = 0; $row < ($numberOfRows * $rowOffset); $row = $row + $rowOffset)
	{
		for(my $col = 0; $col < ($numberOfColumnsPerRow * $colOffset); $col = $col + ($colOffset * $burstLength))
		{
			my $addrHex = sprintf("0x%x", $addr);
			print "$clkCounter:\tread\t$addrHex\t$dataPattern\n";
			$clkCounter++;
			$addr += $colOffset * $burstLength;
		}
	}
}
