Пример #1
0
def assert_all(chip, a):
    """Assert that a stream of data is never 0

    arguments
    ---------

    chip - the chip
    a - the stream of data to check

    returns
    -------

    N/A

    """

    assert_component = Component(C_file="""/* Discard Component */
        int in = input("in");
        void main(){
            while(1){
                assert(fgetc(in));
            }
        }""",
                                 inline=True)

    assert_component(chip, inputs={"in": a}, outputs={}, parameters={})
Пример #2
0
def test():

    chip = Chip("square_root")

    test_in = Stimulus(chip, "input", "float", [i * 0.1 for i in range(100)])
    test_out = Response(chip, "output", "float")

    #create a filter component using the C code
    sqrt = Component("sqrt.c")

    #add an instance to the chip
    sqrt(
        chip,
        inputs={
            "x": test_in,
        },
        outputs={
            "sqrt_x": test_out,
        },
    )

    #run the simulation
    chip.simulation_reset()
    while len(test_out) < 100:
        chip.simulation_step()

    pyplot.plot(list(test_in), list(test_out))
    pyplot.xlim(0, 10.1)
    pyplot.title("Square Root of x")
    pyplot.xlabel("x")
    pyplot.ylabel("$\\sqrt{x}$")
    pyplot.savefig("../docs/source/examples/images/example_1.png")
    pyplot.show()
Пример #3
0
def tee(chip, a, out1=None, out2=None):
    tee_component = Component("""
        int out1 = output("out1");
        int out2 = output("out2");
        int in = input("in");
        void main(){
            int data;
            while(1){
                data = fgetc(in);
                fputc(data, out1);
                fputc(data, out2);
            }
        }""",
                              inline=True)
    if out1 is None:
        out1 = Wire(chip)
    if out2 is None:
        out2 = Wire(chip)
    tee_component(chip,
                  inputs={"in": a},
                  outputs={
                      "out1": out1,
                      "out2": out2
                  },
                  parameters={})
    return out1, out2
Пример #4
0
def cycle(chip, args, type_="int", out=None):

    if out is None:
        out = Wire(chip)

    c_component = """
        #include <stdio.h>
        int out = output("out");
        void main(){
            %s list[%i] = {%s};
            int i;
            %s data;
            while(1){
              for(i=0; i<%i; i++){
                  data = list[i];
                  fput_%s(data, out);
              }
            }
        }
    """ % (type_, len(args), ", ".join(["%s" % i for i in args
                                        ]), type_, len(args), type_)

    cycle_component = Component(c_component, inline=True)

    cycle_component(
        chip,
        inputs={},
        outputs={"out": out},
        parameters={},
    )
    return out
Пример #5
0
def application(chip):

    application = Component("application.c")
    server = Component("server.c")
    arbiter = Component("arbiter.c")

    socket_tx = Wire(chip)
    socket_rx = Wire(chip)
    application_out = Wire(chip)
    server_out = Wire(chip)

    arbiter(
        chip=chip,
        inputs={
            "in1": application_out,
            "in2": server_out,
        },
        outputs={
            "out": chip.outputs["output_rs232_tx"],
        },
    )

    application(
        chip=chip,
        inputs={
            "socket": socket_rx,
            "rs232_rx": chip.inputs["input_rs232_rx"],
            "switches": chip.inputs["input_switches"],
            "buttons": chip.inputs["input_buttons"],
        },
        outputs={
            "socket": socket_tx,
            "rs232_tx": application_out,
            "leds": chip.outputs["output_leds"],
        },
    )

    server(chip=chip,
           inputs={
               "eth_rx": chip.inputs["input_eth_rx"],
               "socket": socket_tx,
           },
           outputs={
               "eth_tx": chip.outputs["output_eth_tx"],
               "rs232_tx": server_out,
               "socket": socket_rx,
           })
Пример #6
0
def application(chip):

    c = Component("application.c")
    c(chip=chip,
      inputs={},
      outputs={
          "rs232_tx": chip.outputs["output_rs232_tx"],
      })
Пример #7
0
def test():

    from math import pi
    from numpy import abs
    from scipy import fft
    from scipy.signal import firwin
    from matplotlib import pyplot
    from chips.api.api import Chip, Stimulus, Response, Wire, Component

    #create a chip
    chip = Chip("filter_example")

    #low pass filter half nyquist 50 tap
    kernel = Stimulus(chip, "kernel", "float",
                      firwin(50, 0.5, window="blackman"))

    #impulse response
    input_ = Stimulus(chip, "input", "float",
                      [1.0] + [0.0 for i in range(1024)])
    output = Response(chip, "output", "float")

    #create a filter component using the C code
    fir_comp = Component("fir.c")

    #add an instance to the chip
    fir_inst_1 = fir_comp(
        chip,
        inputs={
            "a": input_,
            "k": kernel,
        },
        outputs={
            "z": output,
        },
        parameters={
            "N": len(kernel) - 1,
        },
    )

    #run the simulation
    chip.simulation_reset()
    while len(output) < 1024:
        chip.simulation_step()

    #plot the result
    pyplot.semilogy(abs(fft(list(output)))[0:len(output) / 2])
    pyplot.title("Magnitude of Impulse Response")
    pyplot.xlim(0, 512)
    pyplot.xlabel("X Sample")
    pyplot.savefig("../docs/source/examples/images/example_6.png")
    pyplot.show()
Пример #8
0
def test():

    chip = Chip("taylor")

    stimulus = arange(-2 * pi, 2.0 * pi, pi / 25)
    x = Stimulus(chip, "x", "double", stimulus)
    sin_x = Response(chip, "sin_x", "double")
    cos_x = Response(chip, "cos_x", "double")

    #create a filter component using the C code
    sqrt = Component("taylor.c")

    #add an instance to the chip
    sqrt(
        chip,
        inputs={
            "x": x,
        },
        outputs={
            "sin_x": sin_x,
            "cos_x": cos_x,
        },
    )

    #run the simulation
    chip.simulation_reset()
    while len(cos_x) < len(x):
        chip.simulation_step()

    x = list(x)
    sin_x = list(sin_x)[:100]
    cos_x = list(cos_x)[:100]

    pyplot.xticks([-2.0 * pi, -pi, 0, pi, 2.0 * pi],
                  [r'$-2\pi$', r"$-\pi$", r'$0$', r'$\pi$', r'$2\pi$'])
    pyplot.plot(x, sin_x, label="sin(x)")
    pyplot.plot(x, cos_x, label="cos(x)")
    pyplot.ylim(-1.1, 1.1)
    pyplot.xlim(-2.2 * pi, 2.2 * pi)
    pyplot.title("Trig Functions")
    pyplot.xlabel("x (radians)")
    pyplot.legend(loc="upper left")
    pyplot.savefig("../docs/source/examples/images/example_2.png")
    pyplot.show()
Пример #9
0
def async (chip, a, out=None):
    if out is None:
        out = Wire(chip)
    async = Component("""void main(){
        int in = input("in");
        int out = output("out");
        int data;
        while(1){
            if(ready(in)){
                data = fgetc(in);
            }
            if(ready(out)){
                fputc(data, out);
            }
        }
    }""",
                      inline=True)
    async (chip, inputs={"in": a}, outputs={"out": out}, parameters={})
    return out
Пример #10
0
def delay(chip, a, initial=0, type_="int", out=None):
    delay_component = Component("""
        #include <stdio.h>
        int out = output("out");
        int in = input("in");
        void main(){
            fput_%s(INITIAL, out);
            while(1){
                fput_%s(fget_%s(in), out);
            }
        }""" % (type_, type_, type_),
                                inline=True)
    if out is None:
        out = Wire(chip)
    delay_component(chip,
                    inputs={"in": a},
                    outputs={"out": out},
                    parameters={"INITIAL": initial})
    return out
Пример #11
0
def report_all(chip, stream, type_="int"):

    report_all_component = Component("""
        #include <stdio.h>
        int in = input("in");
        void main(){
            while(1){
                report(fget_%s(in));
            }
        }
    """ % type_,
                                     inline=True)

    report_all_component(
        chip,
        inputs={"in": stream},
        outputs={},
        parameters={},
    )
Пример #12
0
def _comparison(chip, a, b, operation, type_="int", out=None):
    if out is None:
        out = Wire(chip)
    code = """
        #include <stdio.h>
        int out = output("out");
        int in1 = input("in1");
        int in2 = input("in2");
        void main(){
            while(1){
                fput_int(fget_%s(in1) %s fget_%s(in2), out);
            }
        }""" % (type_, operation, type_)
    comparison = Component(code, inline=True)
    comparison(chip,
               inputs={
                   "in1": a,
                   "in2": b
               },
               outputs={"out": out},
               parameters={})
    return out
Пример #13
0
def _arithmetic(chip, a, b, operation, type_="int", out=None):
    if out is None:
        out = Wire(chip)
    arithmetic_component = Component("""
        #include <stdio.h>
        /* Adder component model */
        int out = output("out");
        int in1 = input("in1");
        int in2 = input("in2");
        void main(){
            while(1){
                fput_%s(fget_%s(in1) %s fget_%s(in2), out);
            }
        }""" % (type_, type_, operation, type_),
                                     inline=True)
    arithmetic_component(chip,
                         inputs={
                             "in1": a,
                             "in2": b
                         },
                         outputs={"out": out},
                         parameters={})
    return out
Пример #14
0
def test():

    chip = Chip("fft")

    x_re = [0.0 for i in range(1024)]
    x_im = [0.0 for i in range(1024)]
    x_re[0:63] = [sin(2.0 * pi * (i/64.0)) for i in range(64)]

    x_re = Stimulus(chip, "x_re", "double", x_re)
    x_im = Stimulus(chip, "x_im", "double", x_im)
    fft_x_re = Response(chip, "fft_x_re", "double")
    fft_x_im = Response(chip, "fft_x_im", "double")
    
    #create a filter component using the C code
    fft = Component("fft.c")

    #add an instance to the chip
    fft(
        chip, 
        inputs = {
            "x_re":x_re,
            "x_im":x_im,
        },
        outputs = {
            "fft_x_re":fft_x_re,
            "fft_x_im":fft_x_im,
        },
    )

    #run the simulation
    chip.simulation_reset()
    while len(fft_x_im) < len(x_im):
        chip.simulation_step()

    x_re = list(x_re)
    x_im = list(x_im)
    fft_x_re = list(fft_x_re)[:len(x_re)]
    fft_x_im = list(fft_x_im)[:len(x_im)]

    time_complex = [i + (j*1.0) for i, j in zip(x_re, x_im)]
    numpy_complex = s.fft(time_complex)
    numpy_magnitude = n.abs(numpy_complex)

    chips_complex = [i + (j*1.0j) for i, j in zip(fft_x_re, fft_x_im)]
    chips_magnitude = n.abs(chips_complex)

    f, subplot = pyplot.subplots(3, sharex=True)

    pyplot.subplots_adjust(hspace=1.0)
    subplot[0].plot(x_re, 'g')
    subplot[1].plot(numpy_magnitude, 'r')
    subplot[2].plot(chips_magnitude, 'b')
    pyplot.xlim(0, 1023)
    subplot[0].set_title("Time Domain Signal (64 point sine)")
    subplot[1].set_title("Frequency Spectrum - Numpy")
    subplot[2].set_title("Frequency Spectrum - Chips")
    subplot[0].set_xlabel("Sample")
    subplot[1].set_xlabel("Sample")
    subplot[2].set_xlabel("Sample")
    pyplot.savefig("../docs/source/examples/images/example_5.png")
    pyplot.show()
Пример #15
0
def server(chip, ethernet_rx, ethernet_tx, application_in, application_out,
           console_out):

    ip_rx = Component("ip_rx.c", options={"memory_size": 64})
    ip_tx = Component("ip_tx.c", options={"memory_size": 256})
    icmp = Component("icmp.c", options={"memory_size": 1024})
    decoupler = Component("decoupler.c", options={"memory_size": 64})
    tcp_rx = Component("tcp_rx.c", options={"memory_size": 256})
    tcp_tx = Component("tcp_tx.c", options={"memory_size": 1024})

    icmp_in = Wire(chip)
    icmp_out = Wire(chip)
    arp = Wire(chip)
    tcp_in = Wire(chip)
    tcp_out = Wire(chip)
    tcp_decoupler_in = Wire(chip)
    tcp_decoupler_out = Wire(chip)

    stdout = [Wire(chip) for i in range(5)]

    ip_rx(chip=chip,
          inputs={
              "ethernet_in": ethernet_rx,
          },
          outputs={
              "icmp_out": icmp_in,
              "tcp_out": tcp_in,
              "arp": arp,
              "cout": stdout[0],
          },
          parameters={})

    ip_tx(chip=chip,
          inputs={
              "icmp_in": icmp_out,
              "tcp_in": tcp_out,
              "arp": arp,
          },
          outputs={
              "ethernet_out": ethernet_tx,
              "cout": stdout[1],
          },
          parameters={})

    icmp(chip=chip,
         inputs={
             "icmp_in": icmp_in,
         },
         outputs={
             "icmp_out": icmp_out,
             "cout": stdout[2]
         },
         parameters={})

    decoupler(chip=chip,
              inputs={
                  "tcp_in": tcp_decoupler_in,
              },
              outputs={
                  "tcp_out": tcp_decoupler_out,
              },
              parameters={})

    tcp_rx(chip=chip,
           inputs={
               "tcp_in": tcp_in,
           },
           outputs={
               "tcp_out": tcp_decoupler_in,
               "application_out": application_out,
               "cout": stdout[3]
           },
           parameters={})

    tcp_tx(chip=chip,
           inputs={
               "application_in": application_in,
               "tcp_in": tcp_decoupler_out,
           },
           outputs={
               "tcp_out": tcp_out,
               "cout": stdout[4]
           },
           parameters={})

    line_arbiter(chip, stdout, console_out)