@@ -34,6 +34,7 @@ import { showError, showMessage } from './utils'
3434import { showProperties } from './ux'
3535import { useSimulatorMobileStore } from '#/store/simulatorMobileStore'
3636import { toRefs } from 'vue'
37+ import { isTauri } from '@tauri-apps/api/core'
3738
3839var editor
3940var verilogMode = false
@@ -257,56 +258,61 @@ export function YosysJSON2CV(
257258 }
258259}
259260
260- export default function generateVerilogCircuit (
261+ export default async function generateVerilogCircuit (
261262 verilogCode ,
262263 scope = globalScope
263264) {
264265 clearVerilogOutput ( )
265266 setVerilogOutput ( 'Compiling Verilog code...' , 'info' )
266-
267- var params = { code : verilogCode }
268- fetch ( '/api/v1/simulator/verilogcv' , {
269- method : 'POST' ,
270- headers : {
271- 'Content-Type' : 'application/json' ,
272- } ,
273- body : JSON . stringify ( params ) ,
274- } )
275- . then ( ( response ) => {
276- if ( ! response . ok ) {
277- throw response
278- }
279- return response . json ( )
280- } )
281- . then ( ( circuitData ) => {
282- scope . initialize ( )
283- for ( var id in scope . verilogMetadata . subCircuitScopeIds )
284- delete scopeList [ id ]
285- scope . verilogMetadata . subCircuitScopeIds = [ ]
286- scope . verilogMetadata . code = verilogCode
287- var subCircuitScope = { }
288- YosysJSON2CV (
289- circuitData ,
290- globalScope ,
291- 'verilogCircuit' ,
292- subCircuitScope ,
293- true
294- )
295- changeCircuitName ( circuitData . name )
296- showMessage ( 'Verilog Circuit Successfully Created' )
297- setVerilogOutput ( 'Verilog Circuit Successfully Created' , 'success' )
298- } )
299- . catch ( ( error ) => {
300- if ( error . status == 500 ) {
301- showError ( 'Could not connect to Yosys' )
302- setVerilogOutput ( 'Could not connect to Yosys server' , 'error' )
303- } else {
304- showError ( 'There is some issue with the code' )
305- error . json ( ) . then ( ( errorMessage ) => {
306- setVerilogOutput ( errorMessage . message , 'error' )
307- } )
308- }
267+
268+ let apiUrl = '/api/v1/simulator/verilogcv'
269+ let fetchFn = window . fetch
270+
271+ if ( isTauri ( ) ) {
272+ apiUrl = `https://circuitverse.org${ apiUrl } `
273+ try { fetchFn = ( await import ( '@tauri-apps/plugin-http' ) ) . fetch } catch ( e ) { }
274+ }
275+
276+ try {
277+ const response = await fetchFn ( apiUrl , {
278+ method : 'POST' ,
279+ headers : { 'Content-Type' : 'application/json' } ,
280+ body : JSON . stringify ( { code : verilogCode } ) ,
309281 } )
282+
283+ if ( ! response . ok ) throw response
284+ if ( ! response . headers . get ( 'content-type' ) ?. includes ( 'application/json' ) )
285+ throw new Error ( 'Invalid JSON response' )
286+
287+ const circuitData = await response . json ( )
288+
289+ scope . initialize ( )
290+ for ( var id in scope . verilogMetadata . subCircuitScopeIds )
291+ delete scopeList [ id ]
292+ scope . verilogMetadata . subCircuitScopeIds = [ ]
293+ scope . verilogMetadata . code = verilogCode
294+ var subCircuitScope = { }
295+ YosysJSON2CV (
296+ circuitData ,
297+ globalScope ,
298+ 'verilogCircuit' ,
299+ subCircuitScope ,
300+ true
301+ )
302+ changeCircuitName ( circuitData . name )
303+ showMessage ( 'Verilog Circuit Successfully Created' )
304+ setVerilogOutput ( 'Verilog Circuit Successfully Created' , 'success' )
305+ } catch ( error ) {
306+ console . error ( 'Verilog compilation error:' , error )
307+ showError ( 'Verilog compilation failed' )
308+
309+ let msg = error . message || 'Unknown error'
310+ if ( error . status === 500 ) msg = 'Could not connect to Yosys server'
311+ else if ( typeof error . json === 'function' ) {
312+ try { msg = ( await error . json ( ) ) . message || msg } catch ( e ) { }
313+ }
314+ setVerilogOutput ( msg , 'error' )
315+ }
310316}
311317
312318export function setupCodeMirrorEnvironment ( ) {
0 commit comments