Class: CPU

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb,
lib/HDLRuby/hdr_samples/sw_encrypt_cpusim_bench.rb

Overview

A generic CPU description

Direct Known Subclasses

CPUSimu, MEI8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dwidth, awidth, clk, rst) ⇒ CPU

Creates a new generic CPU whose data bus is +dwidth+ bit wide, address bus is +awidth+ bit wide, clock is +clk+, reset +rst+.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 28

def initialize(dwidth,awidth,clk,rst)
    # Check and set the word and address bus widths
    awidth = awidth.to_i
    dwidth = dwidth.to_i
    @awidth = awidth
    @dwidth = dwidth
    # Check and set the signals.
    @clk = clk.to_ref
    @rst = rst.to_ref
    # The allocator of the CPU
    @allocator = Allocator.new(0..(2**@addr),@data)

    # Declare the address and data buses and the
    # rwb/req/ack control signals
    abus,dbus    = nil,nil
    rwb,req,ack  = nil,nil,nil
    # Declares the data and address bus.
    HDLRuby::High.cur_system.open do
        abus = [awidth].input(HDLRuby.uniq_name)
        dbus = [dwidth].input(HDLRuby.uniq_name)
        rwb  = input(HDLRuby.uniq_name)
        req  = input(HDLRuby.uniq_name)
        ack  = output(HDLRuby.uniq_name)
    end
    @abus,@dbus    = abus,dbus
    @rwb,@req,@ack = rwb,req,ack
end

Instance Attribute Details

#abusObject (readonly)

The address bus



16
17
18
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 16

def abus
  @abus
end

#ackObject (readonly)

The acknowledge



24
25
26
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 24

def ack
  @ack
end

#allocatorObject (readonly)

Allocator assotiated with the bus of the CPU



8
9
10
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 8

def allocator
  @allocator
end

#clkObject (readonly)

The clock.



11
12
13
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 11

def clk
  @clk
end

#dbusObject (readonly)

The data bus



18
19
20
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 18

def dbus
  @dbus
end

#reqObject (readonly)

The request



22
23
24
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 22

def req
  @req
end

#rstObject (readonly)

The reset.



13
14
15
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 13

def rst
  @rst
end

#rwbObject (readonly)

The read/!write selection



20
21
22
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 20

def rwb
  @rwb
end

Instance Method Details

#connect(sig) ⇒ Object

Connect signal +sig+ to the bus allocating an address to access it.



57
58
59
60
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 57

def connect(sig)
    # Allocates the signal in the address space.
    @allocator.allocate(sig)
end

#controllerObject

Generates the bus controller.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 63

def controller
    clk,rst,req,ack = @clk,@rst,@req,@ack
    abus,dbus,rwb   = @abus,@dbus,@rwb
    allocator       = @allocator
    HDLRuby::High.cur_system.open do
        par(clk) do
            # Bus controller
            hcase(abus)
            hif(req) do
                ack <= 1
                allocator.each do |sig,addr|
                    hwhen(addr) do
                        hif(rwb) { dbus <= sig }
                        helse    { sig <= dbus }
                    end
                end
            end
            helse do
                ack <= 0
            end
        end
    end

    ## Generates a read of sig executing +ruby_block+ on the result.
    def read(sig,&ruby_block)
        addr = @allocator.get(sig)
        hif(ack == 0) do
            @abus <= addr
            @rwb <= 1
            @req <= 1
        helse
            @req <= 0
            ruby_block.call(@dbus)
        end
    end

    ## Generates a write +val+ to +sig+ executing +ruby_block+
    #  in case of success.
    def write(val,sig,&ruby_block)
        addr = @allocator.get(sig)
        hif(ack == 0) do
            @abus <= addr
            @dbus <= val
            @rwb <= 0
            @req <= 1
        helse
            @req <= 0
            ruby_block.call
        end
    end
end

#read(sig, &ruby_block) ⇒ Object

Generates a read of sig executing +ruby_block+ on the result.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 87

def read(sig,&ruby_block)
    addr = @allocator.get(sig)
    hif(ack == 0) do
        @abus <= addr
        @rwb <= 1
        @req <= 1
    helse
        @req <= 0
        ruby_block.call(@dbus)
    end
end

#write(val, sig, &ruby_block) ⇒ Object

Generates a write +val+ to +sig+ executing +ruby_block+ in case of success.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/HDLRuby/hdr_samples/sw_encrypt_cpu_bench.rb', line 101

def write(val,sig,&ruby_block)
    addr = @allocator.get(sig)
    hif(ack == 0) do
        @abus <= addr
        @dbus <= val
        @rwb <= 0
        @req <= 1
    helse
        @req <= 0
        ruby_block.call
    end
end