<file_sep>#!/usr/bin/env python
from __future__ import print_function
import os
import os.path
import tempfile
import subprocess
import time
import signal
import re
import sys
import shutil

create = 0
log = 0
debug = 0

file_locations = os.path.expanduser(os.getcwd())
logisim_location = os.path.join(os.getcwd(),"../../../../Utilities/logisim.jar")
if log:
  new = open('new.out', 'w')
  logfile = open('TEST_LOG','w')


def student_reference_match_unbounded(student_out, reference_out):
  while True:
    line1 = student_out.readline()
    line2 = reference_out.readline()
    if line2 == '':
      break
    if line1 != line2:
      return False
  return True

def assure_path_exists(path):
        dir = os.path.dirname(path)
        if not os.path.exists(dir):
                os.makedirs(dir)      

class TestCase(object):
  def __init__(self, circfile, outfile, tracefile, points):
    self.circfile  = circfile
    self.outfile = outfile
    self.tracefile = tracefile
    self.points = points

class AbsoluteTestCase(TestCase):
  """
  All-or-nothing test case.
  """
  def __call__(self):
    output = tempfile.TemporaryFile(mode='r+')
    try:
        stdinf = open('/dev/null')
    except Exception as e:
        if debug:
            print("/dev/null not found, attempting different dir..")
        try:
            stdinf = open('nul')
            if debug:
                print("nul dir works!")
        except Exception as e:
            print("The no nul directories. Program will most likely error now.")
    proc = subprocess.Popen(["java","-jar",logisim_location,"-tty","table",os.path.join('.',os.path.basename(self.circfile))],
                            stdin=stdinf,
                            stdout=subprocess.PIPE)
    try:
      assure_path_exists(self.outfile)
      outfile = open(self.outfile, "wb")
      student_out = proc.stdout.read()
      outfile.write(student_out)
      outfile.close()
      assure_path_exists(self.outfile)
      outfile = open(self.outfile, "r")
      reference = open(self.tracefile)
      passed = student_reference_match_unbounded(outfile, reference)
    finally:
      try:
        os.kill(proc.pid,signal.SIGTERM)
      except Exception as e:
         if debug:
            print("Could not kill process! Perhaps it closed itself?")
    if passed:
      return (self.points,"Matched expected output")
    else:
      return (0,"Did not match expected output")



def test_submission(name,outfile,tests):
  # actual submission testing code
  print ("Testing submission for %s..." % name)
  total_points = 0
  total_points_received = 0
  tests_passed = 0
  tests_partially_passed = 0
  tests_failed = 0
  test_results = []
  for description,test,points_received,reason in ((description,test) + test() for description,test in tests): # gross
    points = test.points
    assert points_received <= points
    if points_received == points:
      print ("\t%s PASSED test: %s" % (name, description))
      if log:
        print ("\t%s PASSED test: %s" % (name, description), file=logfile)

      total_points += points
      total_points_received += points
      tests_passed += 1
      test_results.append("\tPassed test \"%s\" worth %d points." % (description,points))
    elif points_received > 0:
      print ("\t%s PARTIALLY PASSED test: %s" % (name,description))
      if log:
        print ("\t%s PARTIALLY PASSED test: %s" % (name,description), file=logfile)
      total_points += points
      total_points_received += points_received
      tests_partially_passed += 1
      test_results.append("\tPartially passed test \"%s\" worth %d points (received %d)" % (description, points, points_received))
    else:
      print ("\t%s FAILED test: %s" % (name, description))
      if log:
        print ("\t%s FAILED test: %s" % (name, description), file=logfile)
      total_points += points
      tests_failed += 1
      test_results.append("\tFailed test \"%s\" worth %d points. Reason: %s" % (description, points, reason))
  
  print ("\tScore for %s: %d/%d (%d/%d tests passed, %d partially)" %\
    (name, total_points_received, total_points, tests_passed, 
     tests_passed + tests_failed + tests_partially_passed, tests_partially_passed))
  print ("%s: %d/%d (%d/%d tests passed, %d partially)" %\
    (name, total_points_received, total_points, tests_passed,
     tests_passed + tests_failed + tests_partially_passed, tests_partially_passed), file=outfile)
  if log:
    print ("\n\n%s: %d/%d (%d/%d tests passed, %d partially)" %\
    (name, total_points_received, total_points, tests_passed,
     tests_passed + tests_failed + tests_partially_passed, tests_partially_passed), file=logfile)
  for line in test_results:
    print (line, file=outfile)
    if log:
      print ( line, file=logfile)
  
  #return points_received
  return tests_failed + tests_partially_passed > 0



def main(tests):
  e = test_submission('correctness',sys.stdout,tests)
  exit(e)
<file_sep># RISCPU-V
[![Build Status](https://travis-ci.org/ThaumicMekanism/RISCPU-V.svg?branch=master)](https://travis-ci.org/ThaumicMekanism/RISCPU-V)

This project is to create a full functioning logisim template of a RISC-V CPU. Eventually, the goal is to make all 16, 32, 64, and 128 bit full functioning cpus.

This project draws inspiration from UC Berkeley's CS61C's class and may contain diagrams from lectures illustrating the circuitry. 

## TODO
	* Work on implementing the CSR register
	* Add more tests to confirm that everything is working as intended<file_sep>#!/usr/bin/env python

import autograder_base
import os.path

from autograder_base import file_locations, AbsoluteTestCase, main

tests = [
  ("Pipelined CPU add/lui/sll test",AbsoluteTestCase(os.path.join(file_locations,'CPU-add_lui_sll.circ'), os.path.join(file_locations,'output/CPU-add_lui_sll.out'), os.path.join(file_locations,'reference_output/CPU-add_lui_sll.out'),1)),
  ("Pipelined CPU memory test",AbsoluteTestCase(os.path.join(file_locations,'CPU-mem.circ'), os.path.join(file_locations,'output/CPU-mem.out'), os.path.join(file_locations,'reference_output/CPU-mem.out'),1)),
  ("Pipelined CPU branch test",AbsoluteTestCase(os.path.join(file_locations,'CPU-branch.circ'), os.path.join(file_locations,'output/CPU-branch.out'), os.path.join(file_locations,'reference_output/CPU-branch.out'),1)),
  ("Pipelined CPU jump test",AbsoluteTestCase(os.path.join(file_locations,'CPU-jump.circ'), os.path.join(file_locations,'output/CPU-jump.out'), os.path.join(file_locations,'reference_output/CPU-jump.out'),1)),
  ("Pipelined CPU swge test",AbsoluteTestCase(os.path.join(file_locations,'CPU-swge.circ'), os.path.join(file_locations,'output/CPU-swge.out'), os.path.join(file_locations,'reference_output/CPU-swge.out'),1)),
  ("Pipelined CPU br_jalr test",AbsoluteTestCase(os.path.join(file_locations,'CPU-br_jalr.circ'), os.path.join(file_locations,'output/CPU-br_jalr.out'), os.path.join(file_locations,'reference_output/CPU-br_jalr.out'),1))
]


if __name__ == '__main__':
  main(tests)
<file_sep>#!/usr/bin/env python3

import os.path
import sys

# This is necessary because we use the same tester for everything.
sys.path.insert(0, '../../../../Utilities')

import autograder_base
from autograder_base import file_locations, AbsoluteTestCase, main

tests = [
  ("Single-cycle CPU add/lui/sll test",AbsoluteTestCase(os.path.join(file_locations,'CPU-add_lui_sll.circ'), os.path.join(file_locations,'output/CPU-add_lui_sll.out'), os.path.join(file_locations,'reference_output/CPU-add_lui_sll.out'),1)),
  ("Single-cycle CPU memory test",AbsoluteTestCase(os.path.join(file_locations,'CPU-mem.circ'), os.path.join(file_locations,'output/CPU-mem.out'), os.path.join(file_locations,'reference_output/CPU-mem.out'),1)),
  ("Single-cycle CPU branch test",AbsoluteTestCase(os.path.join(file_locations,'CPU-branch.circ'), os.path.join(file_locations,'output/CPU-branch.out'), os.path.join(file_locations,'reference_output/CPU-branch.out'),1)),
  ("Single-cycle CPU jump test",AbsoluteTestCase(os.path.join(file_locations,'CPU-jump.circ'), os.path.join(file_locations,'output/CPU-jump.out'), os.path.join(file_locations,'reference_output/CPU-jump.out'),1)),
  ("Single-cycle CPU br_jalr test",AbsoluteTestCase(os.path.join(file_locations,'CPU-br_jalr.circ'), os.path.join(file_locations,'output/CPU-br_jalr.out'), os.path.join(file_locations,'reference_output/CPU-br_jalr.out'),1))
]

if __name__ == '__main__':
  main(tests)<file_sep>#!/bin/bash

# Just run this file and you can test your circ files!
# Make sure your files are in the directory above this one though!
script_full_path=$(dirname "$0")
cd "$script_full_path"
testdir="../tests/cpu_single/"
cp mem.circ $testdir
cp alu.circ $testdir
cp regfile.circ $testdir
cp cpu.circ $testdir
cd $testdir
python3 sanity_test.py
ret=$?
exit $ret
<file_sep>from __future__ import print_function
import sys
hex_size = 8
num_registers = 32
def main(args):
	file = open(args[1])
	lines = [l for l in file.readlines()]
	def mapper(strr):
		return hex(int(strr, 2))[2:]
	results = []
	for l in lines:
		hexes = list(map(mapper,l.split()))
		result = []
		for i in range(num_registers):
			result.append(''.join(hexes[i * hex_size : (i + 1) * hex_size]))
		result.append(''.join(hexes[num_registers * hex_size:num_registers * hex_size + 4]))
		result.append(''.join(hexes[num_registers * hex_size + 4:(num_registers + 1) * hex_size + 4]))
		result.append(''.join(hexes[(num_registers + 1) * hex_size + 4:]))
		results.append(result)
	print ("zero     ra       sp       gp       tp       t0       t1       t2       s0       s1       a0       a1       a2       a3       a4       a5       a6       a7       s2       s3       s4       s5       s6       s7       s8       s9       s10      s11      t3       t4       t5       t6      Line# PC       Inst")
	for r in results:
		string = ' '.join(r)
		print (string)
		print ('\n')


if __name__ == "__main__":
	main(sys.argv)
