Skip to content

Commit

Permalink
Rewrite the code in a more Nim-like fashion
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed May 23, 2019
1 parent da14c2d commit 0529630
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions svSetScope_amvrao/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
import svdpi
import std/[strformat]

const
maxInstances = 100
type
ScopeInfo = object
startAddress: cint
endAddress: cint
scopeName: string

var
currentPointer: cint = 0
startAddress: array[maxInstances, cint]
endAddress: array[maxInstances, cint]
scopes: array[maxInstances, string]
scopes: seq[ScopeInfo]

proc sv_write_reg(address, value: cint) {.importc.}

proc findAddressPointer(address: cint): cint =
# echo &"[findAddressPointer] address = {address}, currentPointer = {currentPointer}"
result = -1.cint
for i in 0.cint .. currentPointer:
if ((address >= startAddress[i]) and (address <= endAddress[i])):
# echo &"[findAddressPointer] match found in instance {i}"
proc findAddressPointer(address: cint): int =
result = -1
for i in 0 .. scopes.len:
if ((address >= scopes[i].startAddress) and (address <= scopes[i].endAddress)):
return i

proc nim_write_reg(address, value: cint) {.exportc.} =
Expand All @@ -30,22 +28,22 @@ proc nim_write_reg(address, value: cint) {.exportc.} =
return

let
currentScopeName = scopes[addrPtr]
echo &"[nim_write_reg] Address {address} found in instance {addrPtr}; scope = {currentScopeName}"
currentScopeName = scopes[addrPtr].scopeName
echo &"[nim_write_reg] Address {address} found in scope = {currentScopeName}"
# set the scope to the specific instance.
discard svSetScope(svGetScopeFromName(currentScopeName.cstring))
sv_write_reg(address, value)
echo ""

proc registerMe(startAddr, endAddr: cint) {.exportc.} =
# echo &"Current pointer = {currentPointer}"
startAddress[currentPointer] = startAddr
endAddress[currentPointer] = endAddr
# Get Scope....
scopes[currentPointer] = $svGetNameFromScope(svGetScope())
echo &"[Scope {scopes[currentPointer]}] Registering addresses {startAddr} to {endAddr}\n"
currentPointer += 1.cint
# echo &"scopes debug: {scopes[0 .. 2]}"
let
currentScopeName = $svGetNameFromScope(svGetScope())
scopes.add(ScopeInfo(startAddress: startAddr,
endAddress: endAddr,
scopeName: currentScopeName))
echo &"[Scope {currentScopeName}] Registering addresses {startAddr} to {endAddr}"
# echo &"scopes = {scopes}"
echo ""

#[
**Original C++ code**
Expand Down

0 comments on commit 0529630

Please sign in to comment.