#include <stdio.h>
#include <sys/mman.h>   // mmap functions
#include <unistd.h>     // POSIX API
#include <errno.h>      // error numbers
#include <stdlib.h>     // exit function
#include <stdint.h>     // type definitions
#include <fcntl.h>      // file control
#include <signal.h>     // catch ctrl-c interrupt signal from parent process
#include <stdbool.h>    // boolean types

#include <hps_arm_a9_0.h> // Platform Designer components addresses

/**********************
* Register offsets
***********************/
/* Remember that each register is offset from each other by 4 bytes;
  this is different from the vhdl view where each register is offset by 1 word
  when we do the addressing on the avalong bus!
  Also note that your offsets might be different depending on how you assigned
  addresses in your qsys wrapper.

  Here we specify the offsets in words rather than bytes because we type cast
  the base address returned by mmap to a uint32_t*. Thus when we increment that
  pointer by 1, the memory address increments by 4 bytes since that's the size
  of the type the pointer points to.
*/

// Define the other register offsets here


// flag to indicate whetehr or not we've recieved an interrupt signal from the OS
static volatile bool interrupted = false;

// graciously handle interrupt signals from the OS
void interrupt_handler(int sig)
{
   printf(
"Received interrupt signal. Shutting down...\n");
   interrupted = true;
}


int main()
{
   
// open /dev/mem
   int devmem_fd = open(
"/dev/mem", O_RDWR | O_SYNC);

   
// check for errors
   if (devmem_fd < 0)
   {
       
// capture the error number
       int err = errno;

       printf(
"ERROR: couldn't open /dev/mem\n");
       printf(
"ERRNO: %d\n", err);

       exit(EXIT_FAILURE);
   }

   
// map our custom component into virtual memory
   
// NOTE: QSYS_LED_CONTROL_0_BASE and QSYS_LED_CONTROL_0_SPAN come from
   
// hps_0_arm_a9_0.h; the names might be different based upon how you
   
// named your component in Platform Designer.
   uint32_t *ALU_base = (uint32_t *) mmap(
NULL, QSYS_ALU_0_SPAN,
       PROT_READ
| PROT_WRITE,MAP_SHARED, devmem_fd, QSYS_ALU_0_BASE);


   
// check for errors
   if (ALU_base == MAP_FAILED)
   {
       
// capture the error number
       int err = errno;

       printf(
"ERROR: mmap() failed\n");
       printf(
"ERRNO: %d\n", err);

       
// cleanup and exit
       close(devmem_fd);
       exit(EXIT_FAILURE);
   }

   
// create pointers for each register
   uint32_t *reg0;
        reg0 = ALU_base;

   uint32_t *reg1;
        reg1 = ALU_base + 0x1;

   uint32_t *reg2;
        reg2 = ALU_base + 0x2;

   uint32_t *reg3;
        reg3 = ALU_base + 0x3;

   uint32_t *reg4;
        reg4 = ALU_base +0x4;

   uint32_t *reg5;
        reg5 = ALU_base +0x5;

        uint32_t *sum;

        sum = ALU_base + 0x6;


   
// Run and print each verification case

        
// Initialization

        *reg0 = 0;
        *reg1 = 0;
        *reg2 = 0;
        *sum = *reg4 + *reg3;

        
// Case 1
           printf(
"\n");
           printf(
"Case 1         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 100;
        *reg1 = 100;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 1;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 2
           printf(
"\n");
           printf(
"Case 2         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 100;
        *reg1 = -100;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 1;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 3
           printf(
"\n");
           printf(
"Case 3         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0x7FFFFFFF;
        *reg1 = 1;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 1;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 4
           printf(
"\n");
           printf(
"Case 4         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 1000;
        *reg1 = 1000;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 2;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 5
           printf(
"\n");
           printf(
"Case 5         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 1000;
        *reg1 = 5000;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 2;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 6
           printf(
"\n");
           printf(
"Case 6         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0x80000000;
        *reg1 = 1;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 2;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 7
           printf(
"\n");
           printf(
"Case 7         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0x0FFFFFFF;
        *reg1 = 0x0FFFFFFF;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 3;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 8
           printf(
"\n");
           printf(
"Case 8         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 =        7000;
        *reg1 = -500;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 3;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 9
           printf(
"\n");
           printf(
"Case 9         R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 7000;
        *reg1 = 0;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 3;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 10
           printf(
"\n");
           printf(
"Case 10        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg1 = 16;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 4;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 11
           printf(
"\n");
           printf(
"Case 11        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg1 = 0x80000000;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 4;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 12
           printf(
"\n");
           printf(
"Case 12        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg1 = 0;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 4;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 13
           printf(
"\n");
           printf(
"Case 13        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 5;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 5;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 14
           printf(
"\n");
           printf(
"Case 14        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 5;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 15
           printf(
"\n");
           printf(
"Case 15        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = -5;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 5;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 16
           printf(
"\n");
           printf(
"Case 16        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = -1;
        *reg1 = 0;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 6;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 17
           printf(
"\n");
           printf(
"Case 17        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 7;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
 
        
// Case 18
           printf(
"\n");
           printf(
"Case 18        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = -1;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 7;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        
// Case 19
           printf(
"\n");
           printf(
"Case 19        R0           R1  R2          R4          R3                   R4+3  R5\n");

           printf(
"IRV: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);
        
        *reg0 = 0xAAAAAAAA;

           printf(
"ABW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

        *reg2 = 7;
        *sum = *reg4 + *reg3;

           printf(
"OPW: ");
        printf(
"%*d ",12,*reg0);
        printf(
"%*d ",12,*reg1);
        printf(
" 0x%08x ", *reg2);
        printf(
" 0x%08x ", *reg4);
        printf(
" 0x%08x ", *reg3);
        printf(
"%*d  ",14,*sum);
        printf(
"0x%08x\n",*reg5);

   
// unmap our custom component
   int result = munmap(ALU_base, QSYS_ALU_0_SPAN);

   
// check for errors
   if (result < 0)
   {
       
// capture the error number
       int err = errno;

       printf(
"ERROR: munmap() failed\n");
       printf(
"ERRNO: %d\n", err);

       
//cleanup and exit
       close(devmem_fd);
       exit(EXIT_FAILURE);
   }

   
// close /dev/mem
   close(devmem_fd);

   return 0;
}