2013年1月22日 星期二

hardare software co-simulation with parallel process + python

底下用 subprocess, 假裝透過 system call 來把底層的 virtual hardware 代起來, 做個假的 simulation. 有點蠢就是了...
virtual hardware
#!/usr/bin/python
# %parallel_proc ["--add"|"--sub"], [--A a], [--B b]]

import argparse

def decoder():
    """ decoder Add, Sub """
    parser = argparse.ArgumentParser(description='Process Proc')
    parser.add_argument('--add', action='store_true', default=False, dest='add', help='add')
    parser.add_argument('--sub', action='store_true', default=False, dest='sub', help='sub')
    parser.add_argument('--A',   action='store', type=int, default=10, dest='a',  help='A')
    parser.add_argument('--B',   action='store', type=int, default=10, dest='b',  help='B')
    parser.add_argument('--t',   action='store', type=int, default=10, dest='t',  help='time')
    args = parser.parse_args()

    if args.add:
     if args.t < 2:
         print "add %d" %(proc_add(args.a, args.b))
 else:
     print "idle %d" %(2)
    elif args.sub:
 if args.t < 3:
     print "sub %d" %(proc_sub(args.a, args.b))
 else:
     print "idle %d" %(3)

def proc_add(a, b):
    return a + b

def proc_sub(a, b):
    return a - b

def main():
    decoder()

if __name__ == '__main__':
    main()

download
#! /usr/bin/python
import subprocess
import sys
import re

reIDLE = re.compile("idle\s+(\w+)", re.M)
reADD  = re.compile("add\s+(\w+)", re.M)
reSUB  = re.compile("sub\s+(\w+)", re.M)

def spawn_add(a=0, b=0, cycle=0):
    proc = subprocess.Popen("./parallel_proc.py --add --A %d --B %d --t %d" %(a, b, cycle), \
             shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    id(proc)
    proc.wait()
    line = proc.stdout.readline()
    idle = reIDLE.findall(line)
    add  = reADD.findall(line)
    ret_idle = 0 if not idle  else idle[0]
    ret_add  = 0 if not add   else add[0]
    return (ret_idle, ret_add)


def spawn_sub(a=0, b=0, cycle=0):
    proc = subprocess.Popen("./parallel_proc.py --sub --A %d --B %d --t %d" %(a, b, cycle), \
             shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    id(proc)
    proc.wait()
    line = proc.stdout.readline()
    idle = reIDLE.findall(line)
    sub  = reADD.findall(line)
    ret_idle = 0 if not idle  else idle[0]
    ret_sub  = 0 if not sub   else sub[0]
    return (ret_idle, ret_sub)


def main():

    # (2+3)+(4-2)
    # time 1 2+3, 4-2
    # time 2 2+3, 4-2
    # time 3    , 4-2
    # time 4  5 , 2

    status = 'start'
    add = { 'a' : 2, 'b' : 3, 'cyc' : 4 }
    sub = { 'a' : 4, 'b' : 2, 'cyc' : 5 }

    def status_inspect(add_idle=0, sub_idle=0):
        if add_idle != 0 and sub_idle != 0 :
            add['cyc'] -= add_idle
     sub['cyc'] -= sub_idle
     status = 'start'
        elif add_idle != 0 and sub_idle == 0:
            add['cyc'] -= add_idle
            status = 'idle_add'
        elif add_idle == 0 and sub_idle != 0:
            sub['cyc'] -= sub_idle
            status = 'idle_sub'
        else:
            return True
        return False

    while True:
 if status == 'start':
    (add_idle, add_val) = spawn_add(add['a'], add['b'], add['cyc'])
    (sub_idle, sub_val) = spawn_sub(sub['a'], sub['b'], sub['cyc'])
    if status_inspect(int(add_idle), int(sub_idle)): break
 elif status == 'idle_add':
    (add_idle, add_val) = spawn_add(add['a'], add['b'], add['cyc'])
    if status_inspect(int(add_idle), int(sub_idle)): break
 elif status == 'idle_sub':
    (sub_idle, sub_val) = spawn_sub(sub['a'], sub['b'], sub['cyc'])
    if status_inspect(int(add_idle), int(sub_idle)): break
 else:
     break

if __name__ == '__main__':
    main()

download

沒有留言:

張貼留言