Skip to content

Commit

Permalink
Implemented and tested memory views.
Browse files Browse the repository at this point in the history
  • Loading branch information
ExpandingMan committed May 17, 2017
1 parent 81669d3 commit ff5df7e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/boilerplate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Here we keep some macros for doing boilerplate code generation...
# TODO deal with page crossings!!!

opfuncname(opcode::UInt8) = Symbol(string("op", hexstring(opcode), "!"))
opstring(fname::Symbol) = strip(string(fname), '!')

assemblydict(opname::Symbol, opcode::UInt8) = :(ASSEMBLY_DICT[$(opstring(opname))] = $opcode)


function opdict(opcode::UInt8, nbytes::Int, ncycles::Int)
Expand Down Expand Up @@ -124,17 +127,16 @@ end






macro opdef(opname::Symbol, defblock::Expr)
@capture(defblock, begin defs__ end)
defs = [tuple(ex.args...) for ex defs]
funcdefs = [opdef(d[1], opname, d[2]) for d defs]
dictries = [opdict(d[2], d[3], d[4]) for d defs]
assemblies = [assemblydict(opname, d[2]) for d defs]
esc(quote
$(funcdefs...)
$(dictries...)
$(assemblies...)
end)
end

12 changes: 12 additions & 0 deletions src/memory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function Base.setindex!(m::Memory, v::AbstractVector{UInt8}, idx::AbstractVector
m.v[idx+one(T)] = v
end

Base.view(m::Memory, idx) = view(m.v, idx + 0x0001)
Base.view(m::Memory, ptr:) = view(m, ptr.addr)

#===================================================================================================
<referencing>
Expand All @@ -55,6 +57,9 @@ dereference{T}(ptr::Π{T}, m::Memory) = m[ptr]
deref(ptr:, m::Memory) = dereference(ptr, m)
()(ptr:, m::Memory) = dereference(ptr, m) # this symbol is \mapsto

# note that the views always return SubArray, even for a single element
derefview(ptr:, m::Memory) = view(m, ptr)

dereference(::Type{UInt8}, ptr:, m::Memory)::UInt8 = dereference(ptr, Π)
deref(::Type{UInt8}, ptr:, m::Memory) = dereference(ptr, Π)

Expand All @@ -75,6 +80,13 @@ function dereference{T}(ptr::Π{T}, m::Memory, ℓ::Integer)
end
deref(ptr:, m::Memory, ℓ::Integer) = dereference(ptr, m, ℓ)

# returns SubArray
function derefview{T}(ptr:{T}, m::Memory, ℓ::Integer)
start_idx = ptr.addr + one(T)
end_idx = start_idx +- 1
view(m.v, start_idx:end_idx)
end

store!{T}(m::Memory, ptr:{T}, val::UInt8) = (m[ptr.addr] = val)
# this stores backwards from the supplied memory address
function storeback!{T}(m::Memory, ptr:{T}, val::UInt16)
Expand Down
8 changes: 8 additions & 0 deletions src/opcodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ bytes2arg(bytes::AbstractVector{UInt8}) = bincat(bytes[1], bytes[2])
# this gets the arguments to the op given the pointer to the instruction and nbytes total
deref_opargs(cs::Chipset, ptr:, nbytes::Integer) = deref(ptr+0x01, cs.ram, nbytes-1)

# this has been tested and so far yields inferior performance
function derefview_opargs(cs::Chipset, ptr:, nbytes::Integer)
derefview(ptr+0x01, cs.ram, nbytes-1)
end


#===================================================================================================
<execution>
Expand Down Expand Up @@ -54,6 +59,9 @@ const OpFunc = FunctionWrapper{UInt8,Tuple{Chipset,Vector{UInt8}}}
# for the time being opcode 0x00 would throw an error
const OPCODES = Vector{Tuple{OpFunc,Int,Int}}(N_OPCODES)

# this is for the assembler
const ASSEMBLY_DICT = Dict{String,UInt8}(); sizehint!(ASSEMBLY_DICT, N_OPCODES)

# format is
# mode, opcode, nbytes, ncycles

Expand Down
1 change: 1 addition & 0 deletions test/opcodes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function makechipset()
cs
end

# TODO investigate allocs!!!

function makebenches()
ref = @benchmarkable begin
Expand Down

0 comments on commit ff5df7e

Please sign in to comment.