Jump to content
pixelpedant

A simple Autohotkey script other MAME users may have use for

Recommended Posts

Here's a simple AutoHotKey script that I'm now using, which others here may have use for here (or might find improvements for), which I'm presently using to automate XB code entry (but potentially, any text entry) into MAME via the Windows clipboard.  For most purposes, making programs available in MAME by saving them in TI Image Tool has been a great solution, for me.  And just entering a program via Classic99, then using it in MAME is an option.  But lately I've been working on a speech program for which accurate speech emulation is naturally at a premium (hence MAME), which is a lot of test/modify/test/modify/test/modify in mid-size batches, and for which assembly routines are being stored along with the program via HMLOADER (so I need to modify it in place).  So I really needed a more efficient way to update 30 lines of code (or whatever) in MAME. 

 

The basic idea is to read the clipboard one character at a time, and

 

1) Enter each character in a way hospitable to the MAME/TI keyboard layout (e.g., ALT+R signifying Fctn+R for "[") and the machine's tolerances with respect to keystroke timing.

2) For newlines, ignore CR, but on LF, press Enter, then wait a little bit for control to return to the user before typing the next line.

 

There's no realistic way to know (from the script's standpoint) how long it will actually take for new keystrokes to be accepted after a line is submitted, so that could be tweaked in the one direction or the other.  The SetKeyDelay timing and the newline Sleep are the major timing decisions.

 

But for the present, this serves as a simple but convenient solution for me, for automating XB program entry (or more like small to mid-size updates) in MAME.  Typing out the contents of the clipboard in MAME while using machine-appropriate keys where necessary and handling newlines.  Maybe someone else will have a use for it.

 

#IfWinActive ahk_exe Mame64.exe
{
Insert::
	SetKeyDelay 25,65
	Loop, parse, clipboard
	{
	switch A_LoopField
		{
		case Chr(34): Send !p
		case "#": Send +{3}
		case "_": Send !{u}
		case "?": Send !{i}
		case "'": Send !{o}
		case "~": Send !{w}
		case "|": Send !{a}
		case "\": Send !{z}
		case "-": Send +{/}
		case "[": Send !{r}
		case "]": Send !{t}
		case Chr(96): Send !{c}
		case "{": Send !{f}
		case "}": Send !{g}
		case Chr(13): Sleep 0
		case Chr(10): 
			{
			Send {Enter}
			Sleep, 550
			}
		default: Send %A_Loopfield%
		}
	}
return
}

 

So for example, this is some code typing itself in MAME, via the clipboard (and the timings in the script above).  Initiated by pressing the Insert key:

 

 

 

  • Like 3

Share this post


Link to post
Share on other sites

Looks good, but you have to be careful when the program gets longer. In TI BASIC, entering a line in a long program takes a lot of time; Extended Basic is much faster, of course.

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

 

An updated, longer version of this script with some basic inspection mainly for the sake of "have I screwed up my offline code entry or code copying" type concerns.  So I can know that once it starts copying, it's going to be copying what at least looks like correctly numbered XB, and if I accidentally initiate copying with inappropriate clipboard contents, it will indicate this and prompt for confirmation.

 

Basically, if there's a fundamental, transparent problem with structure of the clipboard contents from the point of view of pasting as XB, I get this sort of thing:

 

image.png.f8d4dbff0abc96f2a7b33cb25156eea0.png

 

#IfWinActive ahk_exe Mame64.exe
{
Insert::
	lines:=[]
	prev:=0
	Loop, parse, clipboard,`n,`r ; split on LF, omit CR if present
		{
		content:=StrSplit(A_LoopField,A_Space,,2)
		num:=content[1]
		if (num="")
			{
			continue
			}		
		else if num is not digit ; if no valid line number
			{
			mess:="No valid line number for line " A_Index ". in text. Line reads `n`n" Trim(A_Loopfield) "`n`nEnter anyway?"
			MsgBox, 4, , %mess%
			IfMsgBox No
				{
				return
				}
			continue
			}
		else if (lines[num]) ; if line number duplicated
			{
			mess:="Line number " num " used here:`n`n" num A_Space lines[num] "`n`nAlso found here:`n`n " num A_Space content[2] "`n`nEnter anyway?"
			MsgBox, 4, , %mess%
			ifMsgBox No
				{
				return
				}
			}
		else if (num<prev) ; if line numbers out of sequence
			{
			mess:="Line number " prev " precedes line number " num " as follows:`n`n" prev A_Space lines[prev] "`n" num A_Space content[2] "`n`nEnter anyway?"
			MsgBox, 4, , %mess%
			ifMsgBox No
				{
				return
				}			
			}
		lines[num]:=content[2]
		prev:=num
		}
	SetKeyDelay, 25,65 ; pre-press delay and press duration in ms
	Sleep, 300
	Loop, parse, clipboard,,`r
		{
		switch A_LoopField
			{
			case Chr(34): Send !p
			case "#": Send +{3}
			case "_": Send !{u}
			case "?": Send !{i}
			case "'": Send !{o}
			case "~": Send !{w}
			case "|": Send !{a}
			case "\": Send !{z}
			case "-": Send +{/}
			case "[": Send !{r}
			case "]": Send !{t}
			case Chr(96): Send !{c}
			case "{": Send !{f}
			case "}": Send !{g}
			case Chr(10): 
				{
				Send {Enter}
				Sleep, 550 ; newline delay in ms
				}
			default: Send %A_Loopfield%
			}
		}
	Send {Enter} ; optionally, always enter final line even when newline absent
return
}

 

Edited by pixelpedant
  • Like 1

Share this post


Link to post
Share on other sites
On 6/2/2020 at 5:27 PM, pixelpedant said:

Here's a simple AutoHotKey script that I'm now using, which others here may have use for here (or might find improvements for), which I'm presently using to automate XB code entry (but potentially, any text entry) into MAME via the Windows clipboard.  For most purposes, making programs available in MAME by saving them in TI Image Tool has been a great solution, for me.  And just entering a program via Classic99, then using it in MAME is an option.  But lately I've been working on a speech program for which accurate speech emulation is naturally at a premium (hence MAME), which is a lot of test/modify/test/modify/test/modify in mid-size batches, and for which assembly routines are being stored along with the program via HMLOADER (so I need to modify it in place).  So I really needed a more efficient way to update 30 lines of code (or whatever) in MAME. 

 

The basic idea is to read the clipboard one character at a time, and

 

1) Enter each character in a way hospitable to the MAME/TI keyboard layout (e.g., ALT+R signifying Fctn+R for "[") and the machine's tolerances with respect to keystroke timing.

2) For newlines, ignore CR, but on LF, press Enter, then wait a little bit for control to return to the user before typing the next line.

 

There's no realistic way to know (from the script's standpoint) how long it will actually take for new keystrokes to be accepted after a line is submitted, so that could be tweaked in the one direction or the other.  The SetKeyDelay timing and the newline Sleep are the major timing decisions.

 

But for the present, this serves as a simple but convenient solution for me, for automating XB program entry (or more like small to mid-size updates) in MAME.  Typing out the contents of the clipboard in MAME while using machine-appropriate keys where necessary and handling newlines.  Maybe someone else will have a use for it.

 

#IfWinActive ahk_exe Mame64.exe
{
Insert::
	SetKeyDelay 25,65
	Loop, parse, clipboard
	{
	switch A_LoopField
		{
		case Chr(34): Send !p
		case "#": Send +{3}
		case "_": Send !{u}
		case "?": Send !{i}
		case "'": Send !{o}
		case "~": Send !{w}
		case "|": Send !{a}
		case "\": Send !{z}
		case "-": Send +{/}
		case "[": Send !{r}
		case "]": Send !{t}
		case Chr(96): Send !{c}
		case "{": Send !{f}
		case "}": Send !{g}
		case Chr(13): Sleep 0
		case Chr(10): 
			{
			Send {Enter}
			Sleep, 550
			}
		default: Send %A_Loopfield%
		}
	}
return
}

 

So for example, this is some code typing itself in MAME, via the clipboard (and the timings in the script above).  Initiated by pressing the Insert key:

 

 

 

 

Hmmm you are doing what RXB has built in since RXB2001 was introduced and is faster:

 

Share this post


Link to post
Share on other sites
43 minutes ago, RXB said:

Hmmm you are doing what RXB has built in since RXB2001 was introduced and is faster:

 

 

I don't see it that way.. How does RXB allow host machine clipboard paste from MAME? 

 

  • Thanks 1

Share this post


Link to post
Share on other sites
Posted (edited)
11 hours ago, jedimatt42 said:

Is this, this: https://www.autohotkey.com/ ? 

 

I hadn't heard of this thing... this is a pretty cool usage... 

 

Yeah, that's the one.  Quite powerful Windows macroing solution, with support for variables and loops and pretty deep access to the Windows API. 

 

Another script I've been using lately takes a list of either space or comma delimited values (e.g., "250,40,50,76,12,90,81") copied to the clipboard and "pastes" them as equivalent byte values by using the ALT+000 entry method (arbitrarily using ScrollLock for this special paste function).  This will only work in an environment like Notepad++, which will accept and display all control characters.  But it happens to work well for me there when putting together a program containing allophone speech, as I don't (coincidentally) use either CHR$(13) or CHR$(10) (which is to say CR or LF), so it lets me dump allophone strings into a program as raw string data rather than equivalent decimal values.  i.e., like this:

 

 

image.png.7df1e55625e0854936e988d7b2959ff0.png

 

 

Note that CHR$(34) is sent as "" instead of " so that it is parsed correctly when used inside a quoted string within a program. 

 

Also note that the resulting program content cannot at this point (once it contains raw string data including control characters) be pasted into Classic99 or MAME, and must be entered, say, via TI Image Tool.

 

#IfWinActive ahk_exe notepad++.exe
{
ScrollLock::
	{
	Clipboard=%Clipboard%
	Loop, parse, Clipboard,`,` ,`r`n    
		{
		text:=A_Loopfield
		Sleep, 10
		if (text="252")
			{
			charcode:=chr(252)
			Send, {%charcode%}
			}
		else if (text="34")
			{
			Send, `"`"
			}
		else
			{
			Sleep, 5
			Send, {Alt down}
			count:=3-strlen(text)
			Loop, %count%
				{
				text="0"%text%
				}
			Loop, parse, text,
				{
				Send, {Numpad%A_Loopfield%}
				}
			Sleep, 5
			Send, {Alt up}
			}
		}
	return
	}
}

 

Edited by pixelpedant

Share this post


Link to post
Share on other sites

MAME has a incredibly powerful scripting language built in (LUA) that can allow for various interactions with the running machine driver.  I recently started using it and find it very capable (perhaps a bit overkill for what you need it for).

 

More information about LUA in MAME can be found here:

https://docs.mamedev.org/techspecs/luaengine.html

 

Command syntax can be found in the built-in help:

https://github.com/mamedev/mame/blob/master/src/frontend/mame/luaengine.cpp

 

Here's a video of an emulated Color Computer (in MAME) auto-playing a game called Timber Man:

 

... and the LUA code for it:

https://gist.github.com/tlindner/0199ee5ee64ab6fb1e3340856bfdece9

 

Here's sample LUA code that presses keys for a Coco in a MOD Tracker program by Sockmaster:

 

rint("registering callback");
emu.register_start(function()
    coroutine.resume(co)
end)

co = coroutine.create(function ()
    print("waiting to LOAD program")
    emu.wait(3);
    print("sending RUN\"MOD13.BAS\"\n")
    emu.keypost("RUN\"MOD13.BAS\"\n")
    print("waiting to select DRIVE 1")
    emu.wait(15);
    print("selecting DRIVE 1")
    emu.keypost("1");
    print("waiting to select MOD file A")
    emu.wait(10);
    print("selecting MOD file A")
    emu.keypost("A");
    resetcounter = 1
end)

if resetcounter == nil then
    print(coroutine.resume(co))
else
    print("Clear already done")
end

 

  • Like 1

Share this post


Link to post
Share on other sites
6 hours ago, jedimatt42 said:

 

I don't see it that way.. How does RXB allow host machine clipboard paste from MAME? 

 

Sorry I watched the Demo you showed and you can do exactly the same demo with RXB easy.

I guess some other demo would be more to your point.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...