{"slug": "c64u-uci-http-basic-example", "title": "C64U UCI HTTP BASIC EXAMPLE", "summary": "BASIC program example for the C64U (Commodore 64 Ultimate) interface that demonstrates how to perform an HTTP GET request. The code constructs a request to httpbin.org, opens a TCP connection, sends the request, and reads the JSON response body. It includes subroutines for locating the UCI (Ultimate Command Interface) hardware, mapping PETSCII characters to ASCII, and handling network communication errors.", "body_md": "c64http.bas\n\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\n10 rem c64u uci http query tester v1.6\n\n20 print chr$(147):poke646,1:print \"c64u uci http qry v1.6\"\n\n30 rem c() is command bytes, d() is uci reply bytes\n\n40 dim c(260),d(260):dc=260\n\n50 rem endpoint pieces: httpbin echoes query in json args\n\n60 hn$=\"httpbin.org\":qp$=\"codex=basic&uci=1\"\n\n70 pa$=\"/get?\"+qp$:ag$=\"c64u-basic/1.0\"\n\n80 rem find uci, identify network target, open tcp\n\n90 gosub 1000:if er then end\n\n100 co=3:m$=\"uci base \"+str$(ba):gosub 900\n\n110 co=7:m$=\"identify network\":gosub 900\n\n120 c(0)=3:c(1)=1:cl=2:gosub 3000\n\n130 if er then co=2:m$=\"identify failed \"+s$:gosub 900:end\n\n140 co=5:m$=\"network target ok\":gosub 900\n\n150 co=7:m$=\"open tcp \"+hn$+\":80\":gosub 900\n\n160 c(0)=3:c(1)=7:c(2)=80:c(3)=0:cl=4\n\n170 q$=hn$+chr$(0):gosub 2000\n\n180 gosub 3000\n\n190 if er then co=2:m$=\"open failed \"+s$:gosub 900:end\n\n200 if dl<1 then co=2:m$=\"no socket id\":gosub 900:end\n\n210 so=d(0):co=5:m$=\"socket \"+str$(so):gosub 900\n\n220 rem build readable http request from strings\n\n230 co=7:m$=\"build http request\":gosub 900\n\n240 rq$=\"GET \"+pa$+\" HTTP/1.1\"+chr$(13)+chr$(10)\n\n250 rq$=rq$+\"Host: \"+hn$+chr$(13)+chr$(10)\n\n260 rq$=rq$+\"User-Agent: \"+ag$+chr$(13)+chr$(10)\n\n270 rq$=rq$+\"Accept: */*\"+chr$(13)+chr$(10)\n\n280 rq$=rq$+\"Connection: close\"+chr$(13)+chr$(10)+chr$(13)+chr$(10)\n\n290 rem write_socket command: target 3, command 17, socket, payload\n\n300 co=7:m$=\"send http request\":gosub 900\n\n310 c(0)=3:c(1)=17:c(2)=so:cl=3\n\n320 q$=rq$:gosub 2000:wr=cl-3\n\n330 gosub 3000\n\n340 if er then co=2:m$=\"write failed \"+s$:gosub 900:gosub 7000:end\n\n350 if dl<2 then co=2:m$=\"write reply short\":gosub 900:gosub 7000:end\n\n360 ac=d(0)+256*d(1)\n\n370 if ac<wr then co=2:m$=\"short write \"+str$(ac):gosub 900:gosub 7000:end\n\n380 co=5:m$=\"request sent\":gosub 900\n\n390 rem read response in chunks and print only body\n\n400 co=7:m$=\"read response\":gosub 900\n\n410 poke646,1:print \"---- json body ----\"\n\n420 hd=0:hm=0:bt=0:id=0:rc=0\n\n430 c(0)=3:c(1)=16:c(2)=so:c(3)=128:c(4)=0:cl=5\n\n440 gosub 3000\n\n450 if er and left$(s$,2)=\"02\" then id=id+1:goto 530\n\n460 if er and left$(s$,2)=\"01\" then goto 570\n\n470 if er then co=2:m$=\"read failed \"+s$:gosub 900:gosub 7000:end\n\n480 if dl<2 then id=id+1:goto 530\n\n490 ac=d(0)+256*d(1)\n\n500 if ac=0 then id=id+1:goto 530\n\n510 id=0:rc=rc+ac\n\n520 for i=2 to ac+1:x=d(i):gosub 5000:next i\n\n530 if id<60 and rc<4096 then goto 430\n\n540 if rc=0 then co=2:m$=\"no response\":gosub 900:gosub 7000:end\n\n550 if id>=60 then co=2:m$=\"read timeout\":gosub 900:gosub 7000:end\n\n560 goto 580\n\n570 co=5:m$=\"closed by host\":gosub 900\n\n580 gosub 7000\n\n590 co=5:m$=\"done bytes \"+str$(rc):gosub 900\n\n600 poke646,1:end\n\n890 rem sub 900: print m$ using color co for progress/fail status\n\n900 poke646,co:print m$:return\n\n990 rem sub 1000: locate ultimate command interface base register\n\n1000 er=0:ba=57116\n\n1010 if peek(ba+1)=201 then gosub 1100:return\n\n1020 ba=56860\n\n1030 if peek(ba+1)=201 then gosub 1100:return\n\n1040 ba=57340\n\n1050 if peek(ba+1)=201 then gosub 1100:return\n\n1060 co=2:m$=\"uci not enabled\":gosub 900\n\n1070 er=1:return\n\n1090 rem sub 1100: derive uci register addresses from base ba\n\n1100 cs=ba:cd=ba+1:rd=ba+2:sd=ba+3:return\n\n1990 rem sub 2000: append q$ to c() after petscii-to-ascii mapping\n\n2000 for i=1 to len(q$)\n\n2010 x=asc(mid$(q$,i,1)):gosub 2200\n\n2020 c(cl)=x:cl=cl+1:next i:return\n\n2190 rem sub 2200: map one basic string byte x to network wire ascii\n\n2200 if x=13 or x=10 or x=9 or x=0 then return\n\n2210 if x>=193 and x<=218 then x=x-128:return\n\n2220 if x>=65 and x<=90 then x=x+32:return\n\n2230 if x>=97 and x<=122 then return\n\n2240 if x>=32 and x<=126 then return\n\n2250 x=63:return\n\n2990 rem sub 3000: send c() command, collect d() reply and s$ status\n\n3000 er=0:dl=0:sl=0:s$=\"\"\n\n3010 gosub 3400:if er then return\n\n3020 for i=0 to cl-1:poke cd,c(i):next i\n\n3030 poke cs,1\n\n3040 for w=1 to 20000\n\n3050 zt=peek(cs)\n\n3060 if (zt and 128)<>0 then x=peek(rd):if dl<dc then d(dl)=x:dl=dl+1\n\n3070 if (zt and 128)<>0 then goto 3050\n\n3080 if (zt and 64)<>0 then x=peek(sd):if sl<240 then s$=s$+chr$(x):sl=sl+1\n\n3090 if (zt and 64)<>0 then goto 3050\n\n3100 ss=zt and 48\n\n3110 if (zt and 8)<>0 then poke cs,8:er=1:s$=\"uci error\":return\n\n3120 if (zt and 4)<>0 then poke cs,4:goto 3050\n\n3130 if ss=32 or ss=48 then poke cs,2:goto 3050\n\n3140 if ss=0 and (zt and 1)=0 then goto 3180\n\n3150 next w\n\n3160 er=1:s$=\"uci timeout\":return\n\n3180 if sl<2 then er=1:s$=\"no status\":return\n\n3190 if left$(s$,2)<>\"00\" then er=1\n\n3200 return\n\n3390 rem sub 3400: drain old uci data/status and wait for idle\n\n3400 for w=1 to 20000\n\n3410 zt=peek(cs)\n\n3420 if (zt and 128)<>0 then x=peek(rd):goto 3410\n\n3430 if (zt and 64)<>0 then x=peek(sd):goto 3410\n\n3440 ss=zt and 48\n\n3450 if (zt and 8)<>0 then poke cs,8:goto 3410\n\n3460 if (zt and 4)<>0 then poke cs,4:goto 3410\n\n3470 if ss=32 or ss=48 then poke cs,2:goto 3410\n\n3480 if ss=0 and (zt and 1)=0 then return\n\n3490 next w\n\n3500 er=1:s$=\"sync timeout\":return\n\n4990 rem sub 5000: skip http headers until cr/lf/cr/lf, then body\n\n5000 if hd=1 then gosub 6000:return\n\n5010 if hm=0 and x=13 then hm=1:return\n\n5020 if hm=1 and x=10 then hm=2:return\n\n5030 if hm=2 and x=13 then hm=3:return\n\n5040 if hm=3 and x=10 then hd=1:print \"httpbin /get? json:\":return\n\n5050 if x=13 then hm=1:return\n\n5060 hm=0:return\n\n5990 rem sub 6000: print one body byte in readable c64 text\n\n6000 if x=13 then return\n\n6010 if x=10 then print:return\n\n6020 if x=9 then print \" \";:return\n\n6030 if x=123 or x=91 then print:print \"(\";:bt=bt+1:return\n\n6040 if x=125 or x=93 then print:print \")\";:bt=bt+1:return\n\n6050 if x=92 then print \"/\";:bt=bt+1:return\n\n6060 if x=44 then print \",\":return\n\n6070 if x>=97 and x<=122 then x=x-32\n\n6080 if x>=32 and x<=95 then print chr$(x);:bt=bt+1:return\n\n6090 print \".\";:bt=bt+1:return\n\n6990 rem sub 7000: close tcp socket through uci network target\n\n7000 c(0)=3:c(1)=9:c(2)=so:cl=3:gosub 3000:return", "url": "https://wpnews.pro/news/c64u-uci-http-basic-example", "canonical_source": "https://gist.github.com/klumsy/c5006e3c9ae558c7ec442adb9592c019", "published_at": "2026-05-07 07:23:56+00:00", "updated_at": "2026-05-22 11:06:28.430742+00:00", "lang": "en", "topics": ["developer-tools", "open-source", "hardware"], "entities": ["C64U", "UCI", "HTTP", "Commodore 64", "httpbin.org"], "alternates": {"html": "https://wpnews.pro/news/c64u-uci-http-basic-example", "markdown": "https://wpnews.pro/news/c64u-uci-http-basic-example.md", "text": "https://wpnews.pro/news/c64u-uci-http-basic-example.txt", "jsonld": "https://wpnews.pro/news/c64u-uci-http-basic-example.jsonld"}}