/* Author : Rick van der Zwet * S-number : 0433373 * Version : $Id: alu.c 364 2007-12-03 07:23:37Z rick $ * Copyright : FreeBSD Licence * Description : ALU unit */ #include #include #include #include "alu.h" word alu_sra(const word in1, const word in2) { word out = 0; out = in1 >> in2; /* Find out wether high order bit was set */ if (in1 >> (BITSIZE(out) - 1) == 0x1) { /* Set bit in2 till high-order to 1 */ int i = BITSIZE(out) - in2; for (; i < BITSIZE(out); i++) out = out | (1 << i); } return (out); } word alu(word in1, word in2, alu_control_t action) { word out = 0; switch (action) { case ADD: out = in1 + in2; break; case AND: out = in1 & in2; break; case NOR: out = ~(in1 | in2); break; case OR: out = in1 | in2; break; case PassS1: out = in1; break; case PassS2: out = in2; break; case SLL: out = in1 << in2; break; case SRA: out = alu_sra(in1, in2); break; case SRL: out = in1 >> in2; break; case SUB: out = in1 - in2; break; case XOR: out = in1 ^ in2; break; default: fprintf(stderr,"Requested alu-action not implemented\n"); exit(EX_SOFTWARE); break; }; /* Set signals */ if (out < 0) { zero = FALSE; negative = TRUE; } else if (out == 0) { zero = TRUE; negative = FALSE; } else { zero = FALSE; negative = FALSE; } return(out); }