home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 237 / 237.d81 / t.programming1 < prev    next >
Encoding:
Text File  |  2004-01-01  |  17.8 KB  |  697 lines

  1. u
  2.         P R O G R A M M I N G
  3.  
  4.         The Great Adventure of
  5.          Creativity and Logic
  6.            by Dave Moorman
  7.  
  8.  
  9.     We are entering into a strange
  10. world of symbolic logic. Process
  11. Logic, to be exact. If you do those
  12. logic problems found in puzzle
  13. magazines, you know Classic Logic. If
  14. you found Geometry enjoyable in hight
  15. school, you know Proof Logic. And if
  16. you studied philosophic logic in
  17. college, you are aquainted with
  18. Symbolic Logic.
  19.  
  20.     Process Logic is a combination of
  21. Proof Logic and Symbolic Logic --
  22. plus three wonderful additional
  23. features. Like Proof Logic, you will
  24. be arranging statements and commands
  25. in a particular order -- not to prove
  26. some truth, but to affect some action
  27. in the computer. In the process, you
  28. will use Symbolic Logic to manipulate
  29. values.
  30.  
  31.     The three additional features are
  32. symbolic value holders (called
  33. variables and arrays), loops, and
  34. conditional commands. It is the
  35. ability to make conditional changes
  36. in the flow of logic that gives a
  37. computer its ability to "think."
  38.  
  39.     The C-64 includes a built in
  40. BASIC interpreter. Computers are
  41. controlled with three types of
  42. language. At its very heart, the
  43. computer processor recognizes certain
  44. values as "instructions." This is
  45. built into the machine itself, and is
  46. called Machine Language. EVERYTHING
  47. the computer does is done by means of
  48. ML.
  49.  
  50.     ML is nothing but numbers, that
  51. is, numeric values. Remembering such
  52. values and the tasks they perform is
  53. difficult for humans. We need at
  54. least some easily recognizable code
  55. words to remind us about what is
  56. going on. The ML programmer writes
  57. this code and the computer uses a
  58. program to ASSEMBLE that code into ML
  59. -- which is what the computer
  60. actually understands. The code the
  61. programmer writes is called ML, or
  62. more correctly, Assembly Source Code.
  63.  
  64.     But a computer can be smarter
  65. than that. Assembly ML source code
  66. has a one-to-one relationship with
  67. the code the computer understands.
  68. However, the computer can be
  69. programmed to read words, numbers,
  70. and other characters and translate
  71. them into complex groups of ML
  72. instructions. Such a language is
  73. called Compiled. The program that
  74. translates Compiler Code into ML Code
  75. is called a Compiler.
  76.  
  77.     A compiler compiles an entire
  78. program or routine at a time. If the
  79. programmer has made a mistake the
  80. compiler cannot understand, it
  81. reports errors -- but only after
  82. chugging through the whole source
  83. code. So the programmer writes,
  84. compiles, debugs, recompiles,
  85. executes, rewrites, recompiles, etc,
  86. etc. This is an arduous task, to say
  87. the least. It was even mor frustrating
  88. back in the 1960's when the programmer
  89. had to punch cards with each line of
  90. the program and take the "batch" to
  91. the computer room. The operator would
  92. run the batch and return a paper
  93. print-out to the programmer in a few
  94. hours. Or days!
  95.  
  96.     At that time, computers were
  97. finally becoming fairly fast and
  98. powerful. A terminal could be
  99. directly connected to the computer so
  100. the programmer did not have to wait.
  101. But the computer then did a lot of
  102. waiting -- for the programmer's
  103. input. The concept of time-sharing
  104. was developed, where the computer
  105. could switch between many different
  106. terminals, running different programs
  107. at apparently the same time.
  108.  
  109.     To take advantage of time sharing
  110. and to provide a language that was
  111. easy for students to learn and use,
  112. BASIC (standing for Beginner's All
  113. Purpose Symbolic Instruction Code)
  114. was written (invented) in 1963, at
  115. Dartmouth College, by mathematicians
  116. John George Kemeny and Tom Kurtzas.
  117. The commands and math the programmer
  118. typed looked enough like English to
  119. make reading the code relatively
  120. easy.
  121.  
  122.     But the big advantage of BASIC was
  123. that it was  -- and is -- an
  124. Interpreted Language. During the
  125. user's tiny slice of processor time, a
  126. single BASIC statement would be read,
  127. turned into the ML Code necessary to
  128. execute the command, and processed.
  129. Then the processor turned to another
  130. terminal and program to process. On
  131. the BASIC program's next turn, the
  132. next BASIC statement would be
  133. interpreted and executed.
  134.  
  135.     The great thing about an
  136. interpreted language like BASIC is
  137. that the program runs until an error
  138. occurs. Then it stops and delivers
  139. an error message. The programmer can
  140. fix the error and rerun the program.
  141. This made BASIC very interactive. The
  142. programmer did not have to get
  143. everything right before seeing how at
  144. least SOME of the program performed.
  145.  
  146.     In December of 1974, the January
  147. issue of Popular Electronics
  148. published news about the first home
  149. computer -- the Altair 8800. Two
  150. Harvard students, Paul Allen and Bill
  151. Gates saw the magazine -- and their
  152. future. They dropped out of college
  153. and rushed to Albuquerque, NM, where
  154. the Altair was being built.
  155.  
  156.     They realized that these home
  157. computers needed an "operating
  158. system" -- a simple way for users to
  159. interact with the machine. Bill Gates
  160. wrote Altair BASIC using a mainframe
  161. computer with an emulator that made
  162. it act like the Intel 8008
  163. microprocessor used by the Altair.
  164. His BASIC included ML code to read
  165. the keystrokes and put the BASIC
  166. program into memory. Other code would
  167. read BASIC commands and jump to ML
  168. routines that performed them. The
  169. whole thing fit in just 4 kilobytes
  170. of memory (which was rather expensive
  171. at the time).
  172.  
  173.     Gates and his newly founded
  174. company -- MicroSoft -- went on to
  175. write BASIC for nearly every home
  176. computer. BASIC 2 used about 16K of
  177. memory, but was remarkably powerful.
  178. Most anything a programmer wanted to
  179. do could be done in BASIC. True -- it
  180. was slower than straight ML. But it
  181. was easy to learn, faster to write,
  182. and more-or-less portable between
  183. different makes of computers.
  184.  
  185.     When Commodore produced the
  186. Personal Electronic Transactor -- the
  187. PET -- they turned to MicroSoft for
  188. BASIC. Legend has it that Commodore
  189. CEO Jack Tramiel bought BASIC 2.0
  190. outright for $7,000 from cash-
  191. strapped MicroSoft.
  192.  
  193.     So, in the fall of 1981 when
  194. Commodore designed the C-64, they
  195. already owned the BASIC 2.0 operating
  196. system. The C-64 has color video and
  197. other features for which BASIC 2.0
  198. had no commands. But that was OK.
  199. Game designers would certainly use
  200. fast ML for their code. And BASIC 2.0
  201. has commands which can directly read
  202. or write information to places in
  203. memory that can control these
  204. features.
  205.  
  206.     On the up side, the C-64 was
  207. designed to be modified with ML code.
  208. Though BASIC 2.0 was in Read Only
  209. Memory (ROM) and could not be
  210. changed, certain critical jump
  211. locations were in Random Access
  212. Memory (RAM). By changing the jump
  213. addresses, a programmer could add new
  214. commands to BASIC and perform all
  215. sorts of miracles the designers never
  216. dreamed of. The designers did include
  217. "paddle controls" for then popular
  218. games like Break Out. These controls
  219. proved perfect for adding a mouse.
  220.  
  221.     All in all, a C-64 was a
  222. fantastic machine in 1982 when it was
  223. unveiled at the January Consumer
  224. Electronics Show in Las Vegas. Its
  225. capabilities -- especially as a "game
  226. machine" -- and its incredible price
  227. (that dropped below $200 in 1984) kept
  228. it in production through 1992. Over
  229. its decade of manufacture, some 27
  230. million units were sold, making the
  231. C-64 the "Best Selling Computer of the
  232. 20th Century," according to the
  233. revered Guinness Book of World Records
  234. (2000-2001).
  235.  
  236.     And to top of a remarkable (if
  237. often ignored) history, a C-64
  238. Direct-to-TV game joystick was
  239. marketed in 2004 through QVC shopping
  240. channel. Some 200,000 units were sold
  241. between Thanksgiving and Christmas.
  242.  
  243.     Thanks to the designer -- Jeri
  244. Ellsworth -- the computer inside the
  245. joystick is a real, honest-to-
  246. goodness C-64. With nine wires
  247. soldered to the credit-card-sized
  248. board, a user can connect a PS2
  249. keyboard, Commodore disk drive, and an
  250. external power supply.
  251.  
  252.     The Commodore 64 -- more than any
  253. other first-generation, 8-bit
  254. computer -- has proved itself as THE
  255. computer for gamesters and hobbyist
  256. programmers all over the world.
  257.  
  258.  
  259. INSIDE the C-64
  260.  
  261.     Every computer has three
  262. essential parts --
  263.  
  264.  1. A processor which executes ML
  265.     instructions and does math and
  266.     logic operations.
  267.  
  268.  2. Input/Output capabilities -- for
  269.     keyboard, mouse, joystick,
  270.     printers, and disk drives.
  271.  
  272.  3. Memory -- "itty-bitty boxes"
  273.     called bytes which can each hold a
  274.     value of 0 through 255.
  275.  
  276.     Today's computers can handle up
  277. to 8 bytes at a time, making them
  278. incredibly fast. Such speed is
  279. necessary for processing sound
  280. recording, photo-quality images, and
  281. real-time videos.
  282.  
  283.     The first generation computers
  284. (such as the C-64) had processors
  285. that could handle only one byte at a
  286. time. A byte is composed of eight
  287. bits -- little switches which are on
  288. or off, 1 or 0 -- hence these are
  289. called 8-bit computers.
  290.  
  291.     A two-byte value is used to point
  292. to a particular byte in memory, which
  293. means that an 8-bit computer is
  294. limited to 256 x 256 or 65536 bytes of
  295. memory. One kilobyte is actually 1024
  296. bytes, so 65536/1024 equals 64. Thus
  297. the name Commodore 64.
  298.  
  299.     But the C-64 has more than 64K of
  300. memory. The ROM which holds all the ML
  301. code to make BASIC work is "banked" on
  302. top of RAM. By "flipping" certain bits
  303. in the computer, an ML programmer can
  304. set ROM aside and use the RAM
  305. "underneath." Indeed, most everything
  306. about the built-in operating system
  307. can be ignored, giving the expert
  308. programmer a full 64K of memory with
  309. which to work.
  310.  
  311.     As mentioned before, even though
  312. BASIC 2.0 is powerful, ML programmers
  313. have created a number of BASIC
  314. extensions and ML modules to add
  315. features for BASIC programmers. In
  316. 2004, LOADSTAR featured DotBASIC
  317. which adds 72 commands to BASIC 2.0
  318. -- including full mouse control and
  319. Event Driven programming. Other
  320. modules play music and sound effects
  321. in the background, enable easy access
  322. to bitmap graphics, and even let
  323. ordinary BASIC programmers operate
  324. sprites (movable screen objects) and
  325. do split screen effects.
  326.  
  327.  
  328. THE LIMITATIONS
  329.  
  330.     The 6510 microprocessor in the
  331. C-64 operates at 1 Megahertz -- which
  332. is terribly slow when compared to the
  333. 2-3 Gigahertz Pentiums now on the
  334. market. However, 1 Mhz is 1,000,000
  335. clock cycles per second (which is
  336. really pretty quick!) and the 6510's
  337. instructions are quite efficient
  338. compared to Pentium instructions.
  339.  
  340.     The screen is comprised of 64,000
  341. pixels -- 320 x 200. In multi-color
  342. mode, two bits determine which of
  343. four colors will be displayed as
  344. double-wide pixels (160 x 200). The
  345. result is a bit grainy.
  346.  
  347.     Text characters are all 8 x 8
  348. pixels in size, and can display the
  349. character "cell" color or the
  350. background color. In multi-color text
  351. mode, 4x8 double-wide pixels can
  352. present the character color or one of
  353. three "universal" colors.
  354.  
  355.     The font includes 256 characters,
  356. but the programmer is not limited to
  357. the two built-in 256 character fonts.
  358. With a font editor, each of the 256
  359. characters can show any 8 x 8
  360. combination of pixels. Moreover, the
  361. programmer has eight 24 x 21 pixel
  362. sprites -- movable objects -- that can
  363. be designed and displayed anywhere
  364. without disrupting the screen.
  365.  
  366.     Sprites are easily used for things
  367. like pointer and video game characters
  368. because they can freely move on top of
  369. on-screen characters.
  370.  
  371.     The whole screen can be nudged,
  372. pixel by pixel, in any direction,
  373. enabling smooth scrolling effects --
  374. especially when combined with split
  375. screen capabilities.
  376.  
  377.     Sound is remarkable for a one-chip
  378. device, including three synthesized
  379. voices. The synthesizer's envelope has
  380. Attack, Decay, Sustain, and
  381. Release parameters, and include
  382. various filters and resonance
  383. settings. Waveforms include noise,
  384. sawtooth, triangle, and adjustable
  385. pulse. And, with extreme cleverness,
  386. 4-bit recorded sound can be recorded
  387. and played by the computer.
  388.  
  389. [ROBIN'S BETA NOTE:] "Extreme
  390. cleverness" is a bit of an
  391. exaggeration! Playing 4-bit digis is
  392. actually about the easiest thing you
  393. can do with the SID (just one poke,
  394. repeatedly!) as long as you can handle
  395. CIA coding to make an IRQ or NMI
  396. happen over and over again at a steady
  397. pace.
  398.  
  399. [DAVE'S BETA RESPONSE:] Right, Robin!
  400. the CIA is the Complex Interface
  401. Adaptor, which does all the timing for
  402. the C-64 (IRQ and NMI are the two
  403. forms of forced timing). So the
  404. "cleverness" is to get the timing
  405. smooth.
  406.  
  407.     So while the C-64 has certain
  408. limitations, it was crafted in such a
  409. way that truly capable programmers can
  410. create most anything computational. I
  411. have seen real-time three-dimensional
  412. displays (like DOOM, only very low
  413. resolution), the MGM Lion roar, and
  414. hundreds of other truly amazing sound
  415. and video effects.
  416.  
  417.     But most important, anything one
  418. can do on any computer can be at
  419. least MODELED on the C-64. The model
  420. may be rough, but the concepts,
  421. skills, and personal satisfaction in
  422. accomplishing effects are the same as
  423. with a big computer.
  424.  
  425.     The day may come when you will
  426. want to tackle C, C++, C#, Java, Java
  427. Script, Perl, or Visual Basic on a
  428. PC. EVERYTHING conceptual and logical
  429. learned on the C-64 will apply to any
  430. other computer or language.
  431.  
  432.     The C-64 is where one starts --
  433. as did thousands of today's software
  434. design professionals. And hundreds of
  435. hobbyist programmers still find more
  436. than enough challenge to sit up all
  437. hours of the night hunching over their
  438. C-64s, fixing just one more thing!
  439.  
  440.  
  441. ON TO BASIC 2.0
  442.  
  443.     When you turn on the C-64 (or
  444. launch VICE), the screen displays
  445. some title information, then presents
  446. the word READY.
  447.  
  448.     READY? Ready for what?
  449.  
  450.     Ready for anything you want to
  451. do!
  452.  
  453.  
  454.     Type:
  455.  
  456.     PRINT "HELLO"
  457.  
  458.     and press <RETURN>. (On VICE, the
  459. double-quotes are <Shift-2>, and
  460. <RETURN> is the <ENTER> key.)
  461.  
  462.     The computer immediately complies
  463. -- printing to the screen:
  464.  
  465.     HELLO
  466.  
  467.     Type:
  468.  
  469.     ? 5 + 7 * 10
  470.  
  471.     (VICE: the plus is the <-> key
  472. (next to the <0> key), the asterisk is
  473. the <CLOSE BRACKET> key.) (Remember
  474. to press the <RETURN> which tells the
  475. C-64 to go ahead and do it.)
  476.  
  477.      75
  478.  
  479.     The question mark is short for
  480. PRINT.
  481.  
  482.     In the first example, you printed
  483. a STRING, a group of characters in
  484. order. You marked off the beginning
  485. and end of the string with
  486. double-quotes.
  487.  
  488.     In the second example, you
  489. printed numeric values, multiplied
  490. and added according to mathematic
  491. rules (multiply and divide are
  492. performed first, followed by addition
  493. and subtraction).
  494.  
  495.     Both of these are examples of
  496. Immediate Mode. The C-64 immediately
  497. responded to your commands when the
  498. <RETURN> was pressed.
  499.  
  500.     Now Type:
  501.  
  502.     10 ? "HELLO"
  503.     20 ? 5 + 7 * 10
  504.  
  505. (pressing <RETURN> at the end of each
  506. line).
  507.  
  508.     Nothing happened -- at least not
  509. obviously. But inside the C-64 a lot
  510. has taken place. Type:
  511.  
  512.     LIST
  513.  
  514. and the lines appear again. You have
  515. written these lines in Program Mode.
  516.  
  517.     The difference between Immediate
  518. Mode and Program Mode is very simple:
  519.  
  520.  
  521.     If a NUMBER comes first in the
  522.     line, the line is put in Program
  523.     Memory. You can look at the
  524.     program with LIST.
  525.  
  526.     If a command comes first, the
  527.     line is in Immediate Mode and
  528.     processed immediately.
  529.  
  530. To run your program, Type:
  531.  
  532.     RUN
  533.  
  534.     Wonderful! You have written your
  535. first program!
  536.  
  537.     The number you use at the
  538. beginning of a Program line will
  539. determine where that line occurs in
  540. the program. Type:
  541.  
  542.     15 ? "WORLD"
  543.  
  544. and LIST
  545.  
  546.     10 PRINT "HELLO"
  547.     15 PRINT "WORLD"
  548.     20 PRINT 5 + 7 * 10
  549.  
  550.     Since every program line must
  551. have a number and will be ordered by
  552. its number, it is a good idea to use
  553. 10's as you start writing your
  554. program. Then, if you want to tuck
  555. something in between two existing
  556. lines, you can -- just like you just
  557. did.
  558.  
  559.  
  560. VARIABLES
  561.  
  562.     A variable is a "box" that
  563. contains something. Each variable has
  564. a name, comprised of one or two
  565. letters or a letter and a number.
  566. Longer variable names are OK (unless
  567. they contain a BASIC command word),
  568. but the computer will not "see" more
  569. than the first two characters.
  570.  
  571.     Let's wipe out your first
  572. program. Type:
  573.  
  574.     NEW
  575.  
  576. and LIST
  577.  
  578.     It's gone. Now Type:
  579.  
  580.     10 A1$ = "HELLO"
  581.     20 A2$ = "WORLD"
  582.     30 A1 = 5
  583.     40 A2 = 7
  584.     50 A3 = 10
  585.     100 ? A1$ + A2$
  586.     110 ? A1 + A2 * A3
  587.     120 END
  588.  
  589.     We have two types of data --
  590. string and numeric. So we have two
  591. types of variables -- string and
  592. numeric. A string variable has a
  593. dollar sign after the one or two
  594. characters. A numeric variable
  595. doesn't.
  596.  
  597.     String variables cannot be part
  598. of a math formula -- but a plus sign
  599. will string two or more strings
  600. together. A numeric variable cannot
  601. be strung onto a string -- but they
  602. can be part of a math formula.
  603.  
  604.     The C-64 has the greatest
  605. programming interface ever put on an
  606. 8-bit computer. If you want to change
  607. a program line, all you have to do is
  608.  
  609.   * put the line on the screen,
  610.  
  611.   * move your cursor up to it,
  612.  
  613.   * type your changes onto the line,
  614.  
  615.   * and press <RETURN>.
  616.  
  617.  
  618.     To list one line, include the line
  619. number after the LIST command:
  620.  
  621.     LIST 100
  622.  
  623. Other list possibilities include:
  624.  
  625.     LIST -100  list up to line 100
  626.     LIST 100-  list line 100 and after
  627.     LIST 30-50 list from 30 to 50
  628.  
  629.     As a list is scrolling, you can
  630. slow it down by pressing the <CTRL>
  631. key (<TAB> for VICE), or stop it by
  632. pressing <STOP> (<ESC> for VICE).
  633.  
  634.     List line 100:
  635.  
  636.     100 PRINT A1$ + A2$
  637.  
  638. and change it to
  639.  
  640.     100 PRINT A1$ + ", " + A2$
  641.  
  642. Press <RETURN> then RUN the program.
  643.  
  644.     Play around with this program for
  645. a while! Try all sorts of things. You
  646. can print strings and numerics
  647. together on the same line:
  648.  
  649.     100 PRINT A1$ + ", " + A2$; A1
  650.  
  651.     Use a semicolon to separate stuff
  652. you print. Use a comma to tab items
  653. to columns.
  654.  
  655.     Try this:
  656.  
  657.     90 T = A1 + A2 * A3
  658.     95 ? T;
  659.  
  660. and run the program. Or do this:
  661.  
  662.     80 A1$ = A1$ + A1$
  663.     85 A2$ = A2$ + A1$
  664.  
  665.     Try every combination of
  666. variables and print you can think of.
  667. It is YOUR program. And there is
  668. nothing you can do from the keyboard
  669. that will hurt a C-64! NOTHING!
  670.  
  671.  
  672. ABOUT ERRORS
  673.  
  674.     You have surely seen some errors
  675. by now. Two things about errors:
  676.  
  677.  1. The program stops short at any
  678.     error, and
  679.  
  680.  2. The C-64 always says READY. after
  681.     an error.
  682.  
  683.  
  684.     When you get an error, just list
  685. the line and try to figure out what is
  686. wrong. SYNTAX means that something you
  687. typed is incomprehensible to the
  688. stupid machine. Fix it! Then try
  689. again.
  690.  
  691.  
  692.      This is just the beginning.
  693.       Everything you really need
  694.      to become a C-64 programmer
  695.      will be revealed in Part II!
  696.  
  697.  
  698.