{"id":820,"date":"2015-04-05T19:49:02","date_gmt":"2015-04-05T17:49:02","guid":{"rendered":"http:\/\/lallafa.de\/blog\/?p=820"},"modified":"2015-04-05T19:49:37","modified_gmt":"2015-04-05T17:49:37","slug":"a-lua-shell-for-fs-uae","status":"publish","type":"post","link":"https:\/\/lallafa.de\/blog\/2015\/04\/a-lua-shell-for-fs-uae\/","title":{"rendered":"A lua shell for FS-UAE"},"content":{"rendered":"<p>While <a href=\"http:\/\/fs-uae.net\">FS-UAE<\/a> recently added a scripting interface with a <a href=\"http:\/\/www.lua.org\">Lua<\/a> scripting binding, it only provides capabilities to write scripts with hooks that will be called on certain emulator events. I \u00c2\u00a0hacked this scripting interface and added a Lua <strong>remote shell<\/strong>. With this shell you can connect while the emulator is running and issue commands. I also started to add disk image related functions to the Lua binding. With these features combined I could show off the power of a scripting shell by writing a tool to insert floppy and cd-rom disk images while the emulator is running &#8211; a long awaited and missing feature&#8230;<!--more--><\/p>\n<h3>1. Build FS-UAE with the lua shell<\/h3>\n<p>The lua shell is for now only available as a source code patch and you need to compile a fresh FS-UAE to use it. But that is not too difficult:<\/p>\n<p>The code is hosted on the\u00c2\u00a0<strong>lua<\/strong> branch in my github repository:<\/p>\n<p><a href=\"https:\/\/github.com\/cnvogelg\/fs-uae\/tree\/lua\">https:\/\/github.com\/cnvogelg\/fs-uae\/tree\/lua<\/a><\/p>\n<p>I also submitted the patch as a <a href=\"https:\/\/github.com\/FrodeSolheim\/fs-uae\/pull\/60\">pull request<\/a> for the main line FS-UAE \u00c2\u00a0and hope that Frode will like this feature and include it \ud83d\ude42<\/p>\n<p>Clone this branch and start the compilation with the following options:<\/p>\n<pre>$ .\/bootstrap\r\n$ mkdir build\r\n$ cd build\r\n$ ..\/configure --enable-lua<\/pre>\n<p>Now you can compile it:<\/p>\n<pre>$ make\r\n$ (cd dist\/macosx &amp;&amp; make) \u00c2\u00a0# only on Mac OS X<\/pre>\n<p>This results in a new FS-UAE binary with lua shell support.<\/p>\n<h3>2. Configure and First Start<\/h3>\n<p>You have to enable the lua shell in order to use it.<\/p>\n<p>Either add an option to one of your\u00c2\u00a0<strong>.fs-uae<\/strong> config files:<\/p>\n<pre>lua_shell = 1<\/pre>\n<p>or give the option on the command line:<\/p>\n<pre>fs-uae ... --lua_shell=1<\/pre>\n<p>The lua shell opens a TCP\/IP socket on <strong>localhost port 6800<\/strong> and waits for incoming client connects e.g. via telnet or putty.<\/p>\n<p>You can use the following options to change these settings:<\/p>\n<pre>lua_shell_addr = \"localhost\"\r\nlua_shell_port = 6800<\/pre>\n<p>Now launch FS-UAE with the lua shell option enabled and have a look at the log files. They usually reside in <em>Documents\/FS-UAE\/Cache\/Logs\/fs-uae.log.txt.<\/em><\/p>\n<p>There watch out for messages starting with lua-shell:<\/p>\n<pre>$ grep lua-shell ~\/Documents\/FS-UAE\/Cache\/Logs\/fs-uae.log.txt\r\nlua-shell: addr=127.0.0.1, port=6800\r\nlua-shell: +listener: 20\r\nlua-shell: -listener\r\nlua-shell: stopping done...<\/pre>\n<p>If you see these messages you should be able to connect while the emulator is running:<\/p>\n<pre>$ telnet 127.0.0.1 6800\r\nTrying 127.0.0.1...\r\nConnected to localhost.\r\nEscape character is '^]'.\r\nFS-UAE 2.5.29dev Lua 5.2\r\n&gt;<\/pre>\n<p>Ok. Now lets see what you can do in the shell&#8230;<\/p>\n<h3>3. Using the shell<\/h3>\n<p>The lua shell is very similar to the interactive lua interpreter that is shipped with lots of lua distributions (see <a href=\"http:\/\/www.lua.org\/manual\/5.3\/manual.html#7\">doc<\/a>). You can enter a valid lua statement that is then evaluated in the current lua state:<\/p>\n<pre>&gt; print \"hello, world!\"\r\nhello, world!<\/pre>\n<p>Note, that the print command is redirected to your shell. Any return value is not printed automatically, you need to prepend a\u00c2\u00a0<strong>=<\/strong>\u00c2\u00a0(or return) before you can enter an expression:<\/p>\n<pre>&gt; =2+3\r\n5<\/pre>\n<p>Next to most libs that lua already ships the lua shell in FS-UAE also registers special emulator commands for you (see next section for details):<\/p>\n<pre>&gt; =fsuae.floppy.get_num_drives()\r\n1<\/pre>\n<p>This returns the number of virtual floppy drives that are currently emulated.<\/p>\n<p>If you want to quit the shell then enter the quit() command:<\/p>\n<pre>&gt; quit()<\/pre>\n<p>You can also simply disconnect the connection by closing with telnet or putty&#8230;<\/p>\n<h3>3. FS-UAE Lua commands<\/h3>\n<p>Currently, the following modules are defined with commands:<\/p>\n<ul>\n<li>fsemu &#8211; generic emulator commands e.g. for changing display<\/li>\n<li>fsuae &#8211; commands available in the FS-UAE adaption layer of UAE<\/li>\n<li>uae &#8211; core Amiga emulator commands, e.g. read Amiga memory<\/li>\n<\/ul>\n<p>Each module is defined in a lua table with the same name and is thus accessed with this prefix. For an up to date list of commands have a look at the corresponding source files:<\/p>\n<ul>\n<li>fsemu in\u00c2\u00a0<em>libfsemu\/src\/emu\/emu_lua.c<\/em><\/li>\n<li>fsuae in\u00c2\u00a0<em>src\/fs-uae\/lualibfsuae.c<\/em><\/li>\n<li>uae in\u00c2\u00a0<em>src\/lualibuae.cpp<\/em><\/li>\n<\/ul>\n<p>In this post I&#8217;ll focus on the drive image functions I&#8217;ve added:<\/p>\n<pre>fsuae.floppy.get_num_drives()\r\n  return: the number of floppy drives currently active\r\nfsuae.floppy.set_file(num, path)\r\n  num: index of floppy 0..3\r\n  path: file path of drive image (e.g. adf file)\r\n  return: -\r\nfsuae.floppy.get_file(num)\r\n  num: index of floppy 0..3\r\n  return: file path of current drive image or empty string<\/pre>\n<p>The same functions are also available for CD-ROM images: just replace <strong>floppy<\/strong> with <strong>cdrom<\/strong> in the above commands&#8230;<\/p>\n<p>Now you can insert and eject floppy images when you run a lua shell during FS-UAE&#8217;s operation:<\/p>\n<pre>&gt; fsuae.floppy.set_file(0, \"\/path\/to\/my\/test.adf\") \u00c2\u00a0-- insert image into DF0\r\n&gt; fsuae.floppy.set_file(0, \"\") -- eject image in DF0<\/pre>\n<p>You are now able to control your floppies interactively via shell, but there is more&#8230;<\/p>\n<h3>4. fs-uae-ctl Command Line Utility<\/h3>\n<p>You can also write utility programs that use the shell to communicate with FS-UAE while its running.<\/p>\n<p><strong>fs-uae-ctl<\/strong>\u00c2\u00a0found in the new<em> tools<\/em> directory of the FS-UAE source tree is a small Python 3.x tool that allows to manage the floppy and CD-ROM images via command line:<\/p>\n<pre>$ python3 fs-uae-ctl df0 # return the current image attached to DF0\r\ndf0 empty\r\n$ python3 fs-uae-ctl df0 \/path\/to\/my\/test.adf # insert new image\r\n$ python3 fs-uae-ctl df0 eject # remove current image<\/pre>\n<p>Use <strong>df1<\/strong> to <strong>df3<\/strong> to access the other drives (if they are enabled).<\/p>\n<p>Use <strong>cd0<\/strong> to <strong>cd3<\/strong> to access the CD-ROM images.<\/p>\n<p>Some extra options allow you to change the host or port where to find the lua shell. Example:<\/p>\n<pre>... --port 6811 --host my.host.ip<\/pre>\n<h3>5. fs-uae-ctl-ui GUI Image Changer Utility<\/h3>\n<p>Of course you could do more fancy and write a GUI based utility. Here it is: <strong>fs-uae-ctl-ui<\/strong> also found in tools. It requires <a href=\"http:\/\/www.riverbankcomputing.co.uk\/software\/pyqt\/download\">PyQt4<\/a>, so make sure it is installed in your Python 3.x setup. E.g. by installing package\u00c2\u00a0<strong>python3-pyqt4\u00c2\u00a0<\/strong>on a Ubuntu\/Debian Linux system. Mac OS X MaxPorts user install port\u00c2\u00a0<strong>py34-pyqt4<\/strong>.<\/p>\n<p>If you run the tool, you&#8217;ll see a nice image change window:<\/p>\n<div id=\"attachment_822\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-822\" data-attachment-id=\"822\" data-permalink=\"https:\/\/lallafa.de\/blog\/2015\/04\/a-lua-shell-for-fs-uae\/fs-uae-ctl-ui\/\" data-orig-file=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?fit=971%2C518&amp;ssl=1\" data-orig-size=\"971,518\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"fs-uae-ctl-ui\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;fs-uae-ctl-ui: FS-UAE Image Changer Utility&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?fit=300%2C160&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?fit=584%2C312&amp;ssl=1\" class=\"size-medium wp-image-822\" src=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui-300x160.jpg?resize=300%2C160\" alt=\"fs-uae-ctl-ui: FS-UAE Image Changer Utility\" width=\"300\" height=\"160\" srcset=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?resize=300%2C160&amp;ssl=1 300w, https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?resize=150%2C80&amp;ssl=1 150w, https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?resize=400%2C213&amp;ssl=1 400w, https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?w=971&amp;ssl=1 971w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-822\" class=\"wp-caption-text\">fs-uae-ctl-ui: FS-UAE Image Changer Utility<\/p><\/div>\n<p>Usage is really simply:<\/p>\n<p>First press button <strong>Connect<\/strong> to connect with the running FS-UAE lua shell. If all went well then the enabled drives are also enabled in the UI window. See the status line at the bottom for error messages if any.<\/p>\n<p>On an enabled drive slot you can insert a new disk image by entering a new path name into the edit box. If you press button <strong>&#8230;<\/strong> then a file selector will be opened and allows you to choose a new image to be inserted. Pressing the button with the\u00c2\u00a0<strong>Eject Symbol\u00c2\u00a0<\/strong>ejects the image in the drive slot&#8230;<\/p>\n<p>There is also a tab for CD-ROMs with the same feature set.<\/p>\n<h3>6. Dev Tools and more&#8230;<\/h3>\n<p>Both fs-uae-ctl utilities use a common Python 3 library that is also shipped. It allows you to integrate lua shell access to FS-UAE with a few lines of python code and also provides some classes that wrap the floppy and CD-ROM image functions&#8230; A high level<strong> Emu<\/strong> class wraps everything together:<\/p>\n<pre>import fsuae\r\nemu = fsuae.Emu()\r\nif not emu.connect():\r\n  print(\"ERROR\", emu.getError())\r\nelse:\r\n  print(\"Drives\", emu.getNumDrives())\r\n  emu.setFloppyImagePath(0,\"\/path\/to\/my\/test.adf\")\r\n  emu.disconnect()<\/pre>\n<p>So writing your own tool is not really an issue&#8230;<\/p>\n<p>And user<strong> jbl007<\/strong> of the English Amiga Board already picked up the idea while discussing the lua shell (see <a href=\"http:\/\/eab.abime.net\/showthread.php?t=76800\">EAB Thread<\/a>) and created his own tool that offers the image control in a very compact <em>menu attached to the systray icon<\/em> of FS-UAE! See his GitHub Repo:<\/p>\n<p><a href=\"https:\/\/github.com\/sonnenscheinchen\/emu-scripts\">https:\/\/github.com\/sonnenscheinchen\/emu-scripts<\/a><\/p>\n<p>His launcher <strong>fs-uae.py<\/strong> does all the magic&#8230;<\/p>\n<p>(BTW: jbl007 has also written a very nice command line frontend for FS-UE called <a href=\"https:\/\/github.com\/sonnenscheinchen\/amiga\">amiga<\/a>\u00c2\u00a0that allows to run FS-UAE for some standard Amiga models without editing a fs-uae config file. It creates the necessary file automatically during startup&#8230;)<\/p>\n<p>That&#8217;s it&#8230; I hope you enjoy the new lua shell for FS-UAE and if you find new interesting uses of it then drop a comment or join the discussion on EAB&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While FS-UAE recently added a scripting interface with a Lua scripting binding, it only provides capabilities to write scripts with hooks that will be called on certain emulator events. I \u00c2\u00a0hacked this scripting interface and added a Lua remote shell. &hellip; <a href=\"https:\/\/lallafa.de\/blog\/2015\/04\/a-lua-shell-for-fs-uae\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":822,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11,17,9],"tags":[],"class_list":["post-820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-amiga","category-fs-uae","category-software"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2015\/04\/fs-uae-ctl-ui.jpg?fit=971%2C518&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/piBMF-de","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts\/820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/comments?post=820"}],"version-history":[{"count":7,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts\/820\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts\/820\/revisions\/828"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/media\/822"}],"wp:attachment":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/media?parent=820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/categories?post=820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/tags?post=820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}