'
' Windows script for generating arbitary waveform s19 file 
' automaticly.
'
' Todo: add or modify the key points ( turning points ) into/
' in the arrays named "Amplitude" and "Timeline".
'
' This script will auto fill the whole time scale with linear
' value according to the key points.
'
' Copyright Tan Yi, SUNIST LABORATORY
' http://www.sunist.org
' 2005-06-14

' Key points definition, make sure these two array have the 
' same length.
' Modification Needed Here Only!!!!!!!!!!
' ToDo: set the two array to match the waveform you need.
' Amplitude array, unit: Ampere
Dim Amplitude
Amplitude = Array(	0,	75,	75,	99,	99,	0)
' Timeline array, unit: ms
Dim Timeline
Timeline = Array(	0,	2,	4,	6,	18,	20)

' Constants definition.
Const ForReading = 1, ForWriting = 2
Const NMAX = 255	' 8 bits DAC's max digital value.

' ¦¤T=0.05ms, maximum output current when digits equal to NMAX 
' is 100 amperes.
Const DeltaT = 0.05, Imax = 100

' Some variables.
Dim fso, wsh, f, length, org, i, k, j, tmp, MainSource, DataSource, Assembler

' Three key files' name.
MainSource = "ArbitraryWaveformGen.asm"
DataSource = "Waveform.inc"
Assembler = "CASM08Z.EXE"

Set wsh = WScript.CreateObject("WScript.Shell")

' Determine If the two arrays are with same length >= 2
If Not Ubound(Amplitude) = Ubound(Timeline) Or Not Ubound(Amplitude)>=1 _
	Or Not Ubound(Timeline)>=1 Then
	wsh.Popup "Amplitude array and Timeline array must have the same length >= 2£¡", , "Error", 0 + 16
	WScript.Quit
End If

' Determine If the Amplitude array is below the Imax.
For i= 0 to Ubound(Amplitude)
	If Not Amplitude(i) < Imax Or Not Amplitude(i) >= 0 Then
		wsh.Popup "Amplitude must be more than 0 and less than Imax: "&Imax&"A£¡", , "Error", 0 + 16
		WScript.Quit
	End If
Next

' Determine If the Timeline array is increasing.
For i= 1 to Ubound(Timeline)
	If Not Timeline(i) > Timeline(i-1) Then
		wsh.Popup "Timeline must be positive and increasing£¡", , "Error", 0 + 16
		WScript.Quit
	End If
Next


' File objects.
Set fso = CreateObject("Scripting.FileSystemObject")

' Two key file must be in the same directory.
If Not fso.FileExists(MainSource) Or Not fso.FileExists(Assembler) Then
	wsh.Popup "Key file " & MainSource & " or " & Assembler & " not found!", , "Error", 0 + 16
	WScript.Quit
End If

Set f = fso.OpenTextFile(DataSource, ForWriting, True)

' Assembler file head.
f.WriteLine "*"
f.WriteLine "*========== Waveform Data Segment =========="
f.WriteLine "*"

' The waveform array's length.
length = Hex(Timeline(Ubound(Amplitude)) / DeltaT + 1)
f.WriteLine "WAVELENGTH" & vbTab & "EQU" & vbTab & "$" & length
length = Timeline(Ubound(Amplitude)) / DeltaT

' Address of the waveform array.
org = 9000
f.WriteLine vbTab & vbTab & "ORG" & vbTab & "$" & org
f.WriteLine "WAVEFORM"

For i = 1 to Ubound(Timeline)
	' Get the slope of this segment
	k = (Amplitude(i) - Amplitude(i-1)) / (Timeline(i) - Timeline(i-1))
	For j = Timeline(i-1) To Timeline(i) Step DeltaT
		tmp = Amplitude(i-1) + k * (j - Timeline(i-1))
		tmp = tmp / Imax * NMAX
		tmp = Hex(CInt(tmp))
		f.WriteLine vbTab & vbTab & "FCB" & vbTab & "$" & tmp
	Next
Next

' The last DataSource
tmp = Amplitude(Ubound(Timeline))
tmp = tmp / Imax * NMAX
tmp = Hex(CInt(tmp))
f.WriteLine vbTab & vbTab & "FCB" & vbTab & "$" & tmp

f.Close()

' Assemble the new waveform.
wsh.Run Assembler & " " & MainSource & " " & "s", 1, false
WScript.Sleep 1000
wsh.SendKeys "{ENTER}"

' Done.
wsh.Popup "Congratulations! New s19 file including waveform is generated!", 2, "Done", 0 + 64
Set wsh = nothing
WScript.Quit
