@@ -100,6 +100,14 @@ public class ZXProjectBuilder
100100
101101 OutputLogWritter . WriteLine ( "Program built successfully." ) ;
102102
103+ if ( settings . NextMode )
104+ {
105+ if ( ! BuildNexFile ( binary , settings , project , OutputLogWritter ) )
106+ {
107+ return null ;
108+ }
109+ }
110+
103111 return program ;
104112 }
105113 catch ( Exception ex )
@@ -108,6 +116,127 @@ public class ZXProjectBuilder
108116 return null ;
109117 }
110118 }
119+
120+
121+ private static bool BuildNexFile ( byte [ ] binary , ZXBuildSettings settings , ZXProjectManager project , TextWriter outputLogWritter )
122+ {
123+ try
124+ {
125+ outputLogWritter . WriteLine ( "Building .nex file..." ) ;
126+ string binFile = "" ;
127+ string cfgFile = "" ;
128+
129+ // Create .bin file
130+ {
131+ binFile = Path . Combine ( project . ProjectPath , Path . GetFileNameWithoutExtension ( settings . MainFile ) + ".bin" ) ;
132+ if ( File . Exists ( binFile ) )
133+ {
134+ File . Delete ( binFile ) ;
135+ }
136+ File . WriteAllBytes ( binFile , binary ) ;
137+ }
138+
139+ // Create nex.cfg file
140+ {
141+ outputLogWritter . WriteLine ( "Creating nex.cfg configuration file..." ) ;
142+ var sb = new StringBuilder ( ) ;
143+ sb . AppendLine ( "; Minimum core version" ) ;
144+ sb . AppendLine ( "!COR3,0,0" ) ;
145+ // sysvars.inc
146+ {
147+ var sysVarsPath = Path . Combine ( Environment . CurrentDirectory , "Resources" , "sysvars.inc" ) ;
148+ var sysVarsDest = Path . Combine ( project . ProjectPath , "sysvars.inc" ) ;
149+ if ( ! File . Exists ( sysVarsDest ) )
150+ {
151+ File . Copy ( sysVarsPath , sysVarsDest ) ;
152+ }
153+ sb . AppendLine ( "!MMU./sysvars.inc,10,$1C00" ) ;
154+ }
155+ // Origin
156+ {
157+ int org = settings . Origin == null ? 32768 : settings . Origin . Value ;
158+ sb . AppendLine ( string . Format ( "!PCSP${0:X2},${1:X2}" , org , org - 2 ) ) ;
159+ }
160+ // Main file
161+ {
162+ var bank = 5 ;
163+ var address = 0x2000 ;
164+ sb . AppendLine ( string . Format ( ".\\ {0},{1},${2:X2}" ,
165+ Path . Combine ( Path . GetFileNameWithoutExtension ( settings . MainFile ) + ".bin" ) ,
166+ bank ,
167+ address ) ) ;
168+ }
169+ // Save nex.cfg file
170+ {
171+ cfgFile = Path . Combine ( project . ProjectPath , "nex.cfg" ) ;
172+ if ( File . Exists ( cfgFile ) )
173+ {
174+ File . Delete ( cfgFile ) ;
175+ }
176+ File . WriteAllText ( cfgFile , sb . ToString ( ) ) ;
177+ }
178+ }
179+ // Build nex file
180+ {
181+ string nextDriveFolder = "nextdrive" ;
182+ // Delete old .nex file
183+ string nexFile = Path . Combine ( project . ProjectPath , Path . GetFileNameWithoutExtension ( settings . MainFile ) + ".nex" ) ;
184+ if ( File . Exists ( nexFile ) )
185+ {
186+ File . Delete ( nexFile ) ;
187+ }
188+ // Check if nextdata folder exists
189+ string dataFolder = Path . Combine ( project . ProjectPath , nextDriveFolder ) ;
190+ if ( ! Directory . Exists ( dataFolder ) )
191+ {
192+ Directory . CreateDirectory ( dataFolder ) ;
193+ }
194+ // Delete old .next file in data folder
195+ string nexDataFile = Path . Combine ( project . ProjectPath , nextDriveFolder , Path . GetFileNameWithoutExtension ( settings . MainFile ) + ".nex" ) ;
196+ if ( File . Exists ( nexDataFile ) )
197+ {
198+ File . Delete ( nexDataFile ) ;
199+ }
200+
201+ outputLogWritter . WriteLine ( "Building .nex file..." ) ;
202+ Process process = new Process ( ) ;
203+ process . StartInfo . FileName = "python.exe" ;
204+ process . StartInfo . Arguments = string . Format ( "{0} nex.cfg {1}" ,
205+ Path . Combine ( Path . GetDirectoryName ( ZXOptions . Current . ZxbcPath ) , "tools" , "nextcreator.py" ) ,
206+ Path . GetFileNameWithoutExtension ( settings . MainFile ) + ".nex" ) ;
207+ process . StartInfo . WorkingDirectory = project . ProjectPath ;
208+ process . StartInfo . UseShellExecute = false ;
209+ process . StartInfo . CreateNoWindow = true ;
210+ process . StartInfo . RedirectStandardOutput = true ;
211+ process . Start ( ) ;
212+
213+ process . WaitForExit ( ) ;
214+
215+ if ( ! File . Exists ( nexFile ) )
216+ {
217+ outputLogWritter . WriteLine ( "Error building .nex file" ) ;
218+ using ( StreamReader reader = process . StandardOutput )
219+ {
220+ string output = reader . ReadToEnd ( ) ;
221+ outputLogWritter . WriteLine ( output ) ;
222+ }
223+ return false ;
224+ }
225+
226+ // Copy .nex file to data folder
227+ File . Copy ( nexFile , nexDataFile ) ;
228+ }
229+ return true ;
230+ }
231+ catch ( Exception ex )
232+ {
233+ outputLogWritter . WriteLine ( $ "Exception: { ex . Message } { ex . StackTrace } ") ;
234+ return false ;
235+ }
236+
237+ }
238+
239+
111240 public static ZXProgram ? BuildDebug ( TextWriter OutputLogWritter )
112241 {
113242 try
@@ -279,6 +408,12 @@ public class ZXProjectBuilder
279408
280409 OutputLogWritter . WriteLine ( "Program built successfully." ) ;
281410
411+ if ( settings . NextMode )
412+ {
413+ OutputLogWritter . WriteLine ( "Debugging in Next not supported." ) ;
414+ return null ;
415+ }
416+
282417 return program ;
283418 }
284419 catch ( LineOutOfRangeException ex )
@@ -292,6 +427,8 @@ public class ZXProjectBuilder
292427 return null ;
293428 }
294429 }
430+
431+
295432 public static bool Export ( ZXExportOptions Export , TextWriter OutputLogWritter )
296433 {
297434 try
@@ -363,6 +500,8 @@ public static bool Export(ZXExportOptions Export, TextWriter OutputLogWritter)
363500 return false ;
364501 }
365502 }
503+
504+
366505 private static void Cleanup ( string Folder , string ? BinFile = null , string ? DisassemblyFile = null )
367506 {
368507
@@ -383,6 +522,8 @@ private static void Cleanup(string Folder, string? BinFile = null, string? Disas
383522 foreach ( var dir in dirs )
384523 Cleanup ( dir ) ;
385524 }
525+
526+
386527 private static IEnumerable < ZXCodeFile > ScanFolder ( string folder )
387528 {
388529 List < ZXCodeFile > files = new List < ZXCodeFile > ( ) ;
@@ -403,6 +544,8 @@ private static IEnumerable<ZXCodeFile> ScanFolder(string folder)
403544
404545 return files ;
405546 }
547+
548+
406549 private static void OutputProcessLog ( TextWriter OutputLogWritter , Process proc , out string Log )
407550 {
408551 StringBuilder sbLog = new StringBuilder ( ) ;
@@ -433,6 +576,8 @@ private static void OutputProcessLog(TextWriter OutputLogWritter, Process proc,
433576
434577 Log = sbLog . ToString ( ) ;
435578 }
579+
580+
436581 private static bool PreBuild ( bool debug , string path , TextWriter outLog )
437582 {
438583 outLog . WriteLine ( "Building precompilation documents..." ) ;
@@ -449,6 +594,8 @@ private static bool PreBuild(bool debug, string path, TextWriter outLog)
449594
450595 return true ;
451596 }
597+
598+
452599 private static bool PostBuild ( bool debug , string path , ZXProgram CompiledProgram , TextWriter outLog )
453600 {
454601 outLog . WriteLine ( "Building postcompilation documents..." ) ;
0 commit comments