Skip to content

Commit

Permalink
Add extension for loading SPICE files from julia package manager
Browse files Browse the repository at this point in the history
This adds the following extension to ngspice (when loaded from Julia):

In statements that load additional files from the filesystem (e.g.
`.lib` and `.include`), a path preceded by `jlpkg:`, e.g.
`jlpkg:my_pkg/models/my_model.sp` will be resolved by the julia package
manager (taking the first path component to be a package name in
the currently active project). The idea here is to manage libraries
of SPICE files as julia packages.
  • Loading branch information
Keno committed May 9, 2021
1 parent 9928d0f commit af166e9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 33 deletions.
41 changes: 20 additions & 21 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ version = "0.4.1"

[[ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "32a2b8af383f11cbb65803883837a149d10dfe8a"
git-tree-sha1 = "024fe24d83e4a5bf5fc80501a314ce0d1aa35597"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.10.12"
version = "0.11.0"

[[Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "InteractiveUtils", "Reexport"]
git-tree-sha1 = "ac5f2213e56ed8a34a3dd2f681f4df1166b34929"
deps = ["ColorTypes", "FixedPointNumbers", "Reexport"]
git-tree-sha1 = "417b0ed7b8b838aa6ca0a87aadf1bb9eb111ce40"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.12.6"
version = "0.12.8"

[[Dates]]
deps = ["Printf"]
Expand All @@ -45,9 +45,10 @@ deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JLLWrappers]]
git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0"
deps = ["Preferences"]
git-tree-sha1 = "642a199af8b68253517b80bd3bfd17eb4e84df6e"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.2.0"
version = "1.3.0"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
Expand Down Expand Up @@ -93,6 +94,12 @@ uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Preferences]]
deps = ["TOML"]
git-tree-sha1 = "ea79e4c9077208cd3bc5d29631a26bc0cff78902"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.1"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand All @@ -117,9 +124,9 @@ version = "1.0.0"

[[ReplMaker]]
deps = ["REPL", "Unicode"]
git-tree-sha1 = "18bf7b1b917fb7c0eca7e273c3f97ab3e7e717c2"
git-tree-sha1 = "76098218397ec93b925b70ce355144d539b1a8b4"
uuid = "b873ce64-0db9-51f5-a568-4457d8e49576"
version = "0.2.4"
version = "0.2.5"

[[Requires]]
deps = ["UUIDs"]
Expand All @@ -130,12 +137,6 @@ version = "1.1.3"
[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[SafeTestsets]]
deps = ["Test"]
git-tree-sha1 = "36ebc5622c82eb9324005cc75e7e2cc51181d181"
uuid = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
version = "0.0.1"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

Expand All @@ -158,10 +159,6 @@ uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Expand All @@ -179,9 +176,11 @@ uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[ngspice_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "80d2c2004a67cf62d3c24260f86d7bbbaceb16c7"
git-tree-sha1 = "02d25c1bad237d00e23208770c48da1d46f68de1"
repo-rev = "main"
repo-url = "https://github.com/JuliaBinaryWrappers/ngspice_jll.jl"
uuid = "57e080ce-e004-5ece-a144-c95753423b2a"
version = "32.2.0+0"
version = "34.0.0+1"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.1"
[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
ReplMaker = "b873ce64-0db9-51f5-a568-4457d8e49576"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Expand All @@ -20,8 +21,8 @@ Requires = "1.1"
julia = "^1.5"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "SafeTestsets"]
22 changes: 14 additions & 8 deletions src/API/running.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
cmd(command) = ngSpice_Command(command)

init() = (pvoid = convert(Ptr{Nothing}, 0);
ngSpice_Init(gen_psendchar(), gen_psendstat(),
gen_pcontrolledexit(),
gen_psenddata(),
gen_psendinitdata(),
gen_pbgthread(), pvoid))
cmd(command) = ngSpice_Command(command)

function init()
pvoid = convert(Ptr{Nothing}, 0)
ngSpice_Init(gen_psendchar(), gen_psendstat(),
gen_pcontrolledexit(),
gen_psenddata(),
gen_psendinitdata(),
gen_pbgthread(), pvoid)
ngSpice_Init_JLExtensions(
@cfunction(path_resolve, Cstring, (Cstring, Cstring))
)

end

isrunning() = ngSpice_running() # always returns 0

Expand Down
4 changes: 3 additions & 1 deletion src/NgSpice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ foreach(names(@__MODULE__, all=true)) do s
end
end #should this be replaced with aliases

include("extensions.jl")

include("API/get_vector.jl")
include("API/sim_utils.jl")
include("API/running.jl")
Expand All @@ -33,7 +35,7 @@ function __init__()
@require Plots="91a5bcdd-55d7-5caf-9e0b-520d859cae80" @eval include("API/graphs.jl")
end

export get_vector_info,
export get_vector_info,
curplot, listallplots, listallvecs, listcurvecs, getimagvec,
getmagnitudevec, getphasevec, getvec, getrealvec,
NgSpiceGraphs, graph,
Expand Down
30 changes: 30 additions & 0 deletions src/extensions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const JLPATH_PREFIX = "jlpkg:"

function c_strdup(str)
ptr = @ccall libngspice.tmalloc((sizeof(str)+1)::Cint)::Ptr{UInt8}
@GC.preserve str Base.unsafe_copyto!(ptr, pointer(str), sizeof(str))
unsafe_store!(ptr, UInt8(0), sizeof(str) + 1)
ptr
end

function path_resolve(name::Cstring, dir::Cstring)::Cstring
name = unsafe_string(name)
dir = unsafe_string(dir)

if startswith(name, JLPATH_PREFIX)
path = name[sizeof(JLPATH_PREFIX)+1:end]
components = splitpath(path)

@assert components[1] != "/"

pkg_path = Base.locate_package(Base.PkgId(components[1]))
if pkg_path !== nothing
pkg_path = realpath(joinpath(dirname(pkg_path), ".."))
file_path = joinpath(pkg_path, components[2:end]...)
return c_strdup(file_path)
end

return Ptr{Cchar}(-1)
end
return C_NULL
end
18 changes: 16 additions & 2 deletions src/interface/ngspice_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ function ngSpice_Init(printfcn, statfcn, ngexit, sdata, sinitdata, bgtrun, userD
ccall((:ngSpice_Init, libngspice), Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), printfcn, statfcn, ngexit, sdata, sinitdata, bgtrun, userData)
end

function ngSpice_Init_JLExtensions(path_resolve)
ret = try;
ccall((:ngSpice_Init_JLExtensions, libngspice), Cint, (Cint, Ptr{Cvoid},),
1, path_resolve)
catch e;
@warn "Failed to initialize julia extensions. Some features may not work."
return false
end
if ret != 0
error("Failed to initialize julia extensions")
end
return true
end

function ngSpice_Init_Sync(vsrcdat, isrcdat, syncdat, ident, userData)
ccall((:ngSpice_Init_Sync, libngspice), Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cint}, Ptr{Cvoid}), vsrcdat, isrcdat, syncdat, ident, userData)
end
end

function ngSpice_Command(command)
ccall((:ngSpice_Command, libngspice), Cint, (Cstring,), command)
Expand All @@ -16,7 +30,7 @@ end

function ngSpice_Circ(circarray)
ccall((:ngSpice_Circ, libngspice), Cint, (Ptr{Ptr{UInt8}},), circarray)
end
end

function ngSpice_CurPlot()
ccall((:ngSpice_CurPlot, libngspice), Ptr{UInt8}, ())
Expand Down

0 comments on commit af166e9

Please sign in to comment.